Magento – How to send transactional mail with pdf attachment in magento 2

magento2transactional

How can i send transactional mail with pdf attachment in magento 2?
below is my code:

class Post extends \Magento\Framework\App\Action\Action
{
     protected $_objectManager;
     private $_transportBuilder;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
       \Magento\Framework\ObjectManagerInterface $objectManager,
       \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
       \Magento\Framework\Escaper $escaper
    ) {
         $this->_objectManager = $objectManager;    
         $this->_transportBuilder = $transportBuilder;
        parent::__construct($context,$objectManager);
    }   
    public function execute()
    {

        $this->_view->loadLayout();
        $post = $this->getRequest()->getPostValue();
        $model = $this->_objectManager->create('Zalw\Catalogue\Model\Catalogue');
        $name = isset($post['name']) ? $post['name'] : '';
        $address = isset($post['streetaddress']) ? $post['streetaddress'] : '';
        $radio = isset($post['catradio']) ? $post['catradio'] : '';
        $city = isset($post['catcity']) ? $post['catcity'] : '';
        $state = isset($post['state']) ? $post['state'] : '';
        $zip = isset($post['zip']) ? $post['zip'] : '';
        $email = isset($post['email']) ? $post['email'] : '';
        $connect = isset($post['connect']) ? $post['connect'] : 0;
        $model->setData('name',$name);
        $model->setData('street_address', $address);
        $model->setData('customer_type', $radio);
        $model->setData('city', $city);
        $model->setData('state', $state);
        $model->setData('zip', $zip);
        $model->setData('email', $email);
        $model->setData('stay_connected',$connect);
        //$model->save();
        $url = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')
        ->getStore()
        ->getBaseUrl();
        $pdfUrl = $url.'var/uploadPdf/';
        $templateParams=array('customer' => 'test@gmail.com', 'store' => "1",'subject' =>"success message");     
        $transport = $this->_transportBuilder
        ->setTemplateIdentifier('1')
        ->setTemplateOptions(array('area' => \Magento\Framework\App\Area::AREA_FRONTEND,'store' => 1))
        ->setTemplateVars($templateParams)
        ->setFrom(array('name' => 'test','email' => 'test.com'))
        ->addTo('test@gmail.com')
        ->getTransport();
        $transport->sendMessage();


        $this->_redirect('message');
        $this->messageManager->addSuccess(__('Thank you for contacting us. We will mail your catalog shortly.')); 
    }
}

Best Answer

It appears that Magento 2 Email's TransportBuilder class doesn't support email attachments yet.

Since the default Message class inherits from Zend_Mail class, one workaround is to extend the TransportBuilder class and call $message's createAttachment method to send file attachment in email.

You can use following class in place of TransportBuilder class to build Transport object and include attachment.

use Magento\Framework\Mail\MessageInterface;
use Magento\Framework\Mail\TransportInterfaceFactory;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Mail\Template\FactoryInterface;
use Magento\Framework\Mail\Template\SenderResolverInterface;
use Magento\Framework\Mail\Template\TransportBuilder;


class UploadTransportBuilder extends TransportBuilder {
    public function __construct(FactoryInterface $templateFactory,
        MessageInterface $message,
        SenderResolverInterface $senderResolver,
        ObjectManagerInterface $objectManager,
        TransportInterfaceFactory $mailTransportFactory) {

        parent::__construct($templateFactory,
            $message,
            $senderResolver,
            $objectManager,
            $mailTransportFactory);
    }

    public function attachFile($file, $name) {
        if (!empty($file) && file_exists($file)) {
            $this->message
            ->createAttachment(
                file_get_contents($file),
                \Zend_Mime::TYPE_OCTETSTREAM,
                \Zend_Mime::DISPOSITION_ATTACHMENT,
                \Zend_Mime::ENCODING_BASE64,
                basename($name)
                );
            }

        return $this;
    }

}