0

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.')); 
    }
}
3

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;
    }

}
|improve this answer|||||
  • this is not working for me...can you help me – Anand Ontigeri Feb 19 '16 at 11:44
  • What exact problem you are facing with above code? – Sameer Feb 19 '16 at 11:46
  • i am able to send email,unable to send the attachments. – Anand Ontigeri Feb 19 '16 at 12:27
  • i want to send .csv file and receive the same. – Anand Ontigeri Feb 19 '16 at 12:36
  • What specific error/exception you are getting while using above TransportBuilder class? – Sameer Feb 19 '16 at 12:53
1

Add return $this;:

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;
    }
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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