Attaching PDF file to email on Magento 2

attach-pdf-file-email-magento-2

In Magento 2, The lib/internal/Magento/Framework/Mail/Template/TransportBuilder Class takes responsibility for preparing and sending email, but it’s still lacking a method for attaching PDF files.

However, since the TransportBuilder Class uses Message class, which inherits from \Zend_Mail, we are now able to add attachments programmatically.

Here is how we do it.

Step 1: Create TransportBuilder Class

Step 2: Create sample Email Sending on Controller

That’s all for the coding part.

Try to run the controller and check the result on the receiver’s email.

You will see a PDF file in the sent email like in the image below.

Hope this topic is helpful for you. Don’t mind leaving your questions here if you have any problems.