Magento 2.3+ – Attach a PDF on an email programmatically


Adding a custom made PDF inside an email would be something that you might need to develop, which is not standard contemplated by the core TransportBuilder Magento library, but don’t worry, this is not the end of the world, in this post am i going to show you how to proceed and get with an accurate solution.

0- You will need to create the PDF file. Follow this post to do so: Create PDF

1- Create your custom email template

app/code/Offset101/PdfEmail/etc/email_templates.xml

app/code/Offset101/PdfEmail/view/frontend/email/email_with_pdf.html

 

2- Create your TransportBuilder class:

Offset101/PdfEmail/Model/Mail/TransportBuilder.php

app/code/Offset101/PdfEmail/etc/di.xml

 

3- Attach the PDF and send the EMail.

app/code/Offset101/PdfEmail/Controller/Email/Share.php

 

Remember to redeploy the static content, clean cache, and compile your files. Afterwards you will be able to test.

Enjoy!