Attaching PDF file to email on Magento 2

Mai Xuan Truong

Attaching PDF file to email on Magento 2

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

However, since the TransportBuilder Class uses the 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 a Sample Email to Send on Controller

<?php

namespace Vendor_Name\Module_Name\Controller\Adminhtml\Ticket;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Translate\Inline\StateInterface;
use Vendor_Name\Module_Name\Model\Mail\Template\TransportBuilder;
use Magento\Framework\App\Area;
use Magento\Store\Model\StoreManagerInterface;


/**
* Class SendMail
* @package Magenest\Ticket\Controller\Adminhtml\Ticket   ,
*/
class SendMail extends Action
{
   /**
    * @var StateInterface
    */
   protected $inlineTranslation;

   /**
    * @var TransportBuilder
    */
   protected $_transportBuilder;

   /**
    * @var StoreManagerInterface
    */
   protected $_storeManager;

   /**
    * SendMail constructor.
    * @param Context $context
    */
   public function __construct(
       Context $context,
       StateInterface $inlineTranslation,
       TransportBuilder $transportBuilder,
       StoreManagerInterface $storeManager
   ) {
       parent::__construct($context);
       $this->inlineTranslation = $inlineTranslation;
       $this->_transportBuilder = $transportBuilder;
       $this->_storeManager = $storeManager;
   }

   /**
    * @return $this
    */
   public function execute()
   {
       $resultPage = $this->resultRedirectFactory->create();
       $this->sendMail();
       return $resultPage->setPath('*/*/index');
   }
   /**
    * Send Mail to customer
    *
    * @param $eventName
    */
   public function sendMail()
   {
       $pdfFile = '.../email.pdf';
       $this->inlineTranslation->suspend();
       $transport = $this->_transportBuilder->setTemplateIdentifier('name_email_use')->setTemplateOptions(
           [
               'area' => Area::AREA_FRONTEND,
               'store' => $this->_storeManager->getStore()->getId(),
           ]
       )->setTemplateVars(
           [
               'message' => 'this is test with send mail',
           ]
       )->setFrom(
           [
               'email' => 'test@gmail.com',
               'name' => 'Test'
           ]
       )->addTo(
          'receiver@gmail.com', 'Receiver'
       )->addAttachment(file_get_contents($pdfFile))->getTransport();
       $transport->sendMessage();
       $this->inlineTranslation->resume();
   }}

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.

Step 2: Create sample Email Sending on Controller

Hope this topic is helpful for you. Don’t mind contacting our high-level Magento experts for the most advanced solutions if you have any problems.

Latest Insights

How to Start a Shopify Store in Just Over an Hour Course

Don't want to miss out on our latest insights?
Subscribe to our newsletter.

Disclaimer: By clicking submit, you agree to share your information with us to receive news, announcements, and resources when they are available.