0

I would like to attach the invoice PDF to the invoice email. I have searched through several links on Stack Exchange, but unfortunately, I have not been able to make any progress on this issue.

I am aware that customers can download their invoices from their account area. However, I am wondering if there is a way to automatically attach the customer's invoice to the invoice email that we send out.

I hope someone can guide me in the right direction.

Thank you.

2 Answers 2

1

You can use the method createAttachement of Zend_Mail class. Your message should inherit this class. If you get the message from your transport class and add the attachement there it should work.

I would propose the following code - the creation of the invoice pdf is tested and safe, the rest is theoretical :-). Of course it might be usefull to transfer the object creation into constructor and add some error handling.

public function sendMail($emailTemplateVariables,$senderInfo,$receiverInfo,$type,$invoiceId = null)
{
    [...]
    $this->generateTemplate($emailTemplateVariables,$senderInfo,$receiverInfo);
    $transport = $this->_transportBuilder->getTransport();
    //attach the invoice pdf
    $this->attachInvoicePdf($invoiceId);
    $transport->sendMessage();
    $this->inlineTranslation->resume();
}

private function attachInvoicePdf($invoiceId){

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $pdfInvoiceModel = $objectManager->get('\Magento\Sales\Model\Order\Pdf\Invoice');
    $invoice = $objectManager->get('\Magento\Sales\Model\Order\Invoice')->load($invoiceId);
    $pdfContent = $pdfInvoiceModel->getPdf([$invoice])->render();
    $transport = $this->_transportBuilder->getTransport();
    $transport->getMessage()->createAttachment($pdfContent);
    
}
2
  • Can you show me a example I don’t get what you meant by create a method of createAttachment. Am I creating a plugin for this
    – Tj1997
    May 29, 2022 at 11:08
  • It looks like he is showing an example. Looks as to be the attachInvoicePdf() function is good to go. The sendMail() function is where you would need to come up with the [...] to make it work for your store. This code would most likely have to be put in a custom/local extension.
    – treckstar
    Jun 1, 2022 at 3:23
1

You can also take a look at an existing extension: https://fooman.com/magento-extension-email-attachments-m2.html. This extension is used very often and Fooman extensions are of high quality.
I would give this a try.

2
  • This module doesn’t work when you run setup:di:compile
    – Tj1997
    May 31, 2022 at 17:27
  • I honestly think you should try giving this extension another shot. They have a demo of it on their page so it most definitely does work. Maybe take a look at this install guide and maybe it might work for you: magento2-support.fooman.co.nz/article/…
    – treckstar
    Jun 1, 2022 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.