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' => '[email protected]', '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('[email protected]')
 ->getTransport();
 $transport->sendMessage();
 $this->_redirect('message');
 $this->messageManager->addSuccess(__('Thank you for contacting us. We will mail your catalog shortly.')); 
 }
}
2 Answers 2
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;
 }
}
- 
 this is not working for me...can you help meAnand Ontigeri– Anand Ontigeri2016年02月19日 11:44:12 +00:00Commented Feb 19, 2016 at 11:44
- 
 What exact problem you are facing with above code?Sameer– Sameer2016年02月19日 11:46:46 +00:00Commented Feb 19, 2016 at 11:46
- 
 i am able to send email,unable to send the attachments.Anand Ontigeri– Anand Ontigeri2016年02月19日 12:27:38 +00:00Commented Feb 19, 2016 at 12:27
- 
 i want to send .csv file and receive the same.Anand Ontigeri– Anand Ontigeri2016年02月19日 12:36:57 +00:00Commented Feb 19, 2016 at 12:36
- 
 What specific error/exception you are getting while using above TransportBuilder class?Sameer– Sameer2016年02月19日 12:53:56 +00:00Commented Feb 19, 2016 at 12:53
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;
 }