Magento2 Email Attachment – How to Send Email with Attachment in Magento 2 attachmentemailmagento2 How to send email with a file attachment. Best Answer M2 does not come with out of the box however it is a feature built into the zend framework. Here is a good reference how to add this functionality into magento: https://blog.bitexpert.de/blog/sending-mails-with-attachments-in-magento-2/ In case link goes dead, create the following <?php namespace Your\CustomModule\Magento\Mail\Template; class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder { public function addAttachment( $body, $mimeType = Zend_Mime::TYPE_OCTETSTREAM, $disposition = Zend_Mime::DISPOSITION_ATTACHMENT, $encoding = Zend_Mime::ENCODING_BASE64, $filename = null ) { $this->message->createAttachment($body, $mimeType, $disposition, $encoding, $filename); return $this; } } then add to etc/di.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="\Magento\Framework\Mail\Template\TransportBuilder" type="\Your\CustomModule\Magento\Mail\Template\TransportBuilder" /> </config> Now you can use addAttachment() throughout your site. Related SolutionsMagento 1.9 – Send Email Attachment with Zend [SOLVED] I changed my code now its working i also made sure you couldnt upload files bigger then 21mb becuause if you do it doesnt work $mail = new Zend_Mail(); $mail->setBodyText($bericht); $mail->setFrom($email, $name); $mail->addTo("johndoe@example.com", "John Doe"); $mail->setSubject("Snel offerte"); $tmpFilePath = $_FILES['foto']['tmp_name']; if ($tmpFilePath != ""){ $file_size = $_FILES['foto']['size']; if (($file_size > 22020096)){ Mage::getSingleton('core/session')->addError($this->__('Bestand mag niet groter zijn dan 21 MB')); } else{ $fname = $_FILES['foto']['name']; $ftempname = $_FILES['foto']['tmp_name']; $at = new Zend_Mime_Part(file_get_contents($tmpFilePath)); $at->disposition = Zend_Mime::DISPOSITION_INLINE; $at->encoding = Zend_Mime::ENCODING_BASE64; $at->filename = $fname; $mail->addAttachment($at); } } try { $mail->send(); } Magento – How to send transactional mail with csv/excel attachment in magento 2 Replace $transport =$transportBuilder->setTemplateIdentifier($templateId) ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId]) ->setTemplateVars($templateParams) ->setFrom(array('email'=>'anand@gmail.com', 'name'=>'anand')) ->addTo('anand.ontigeri@alshaya.com','anand.ontigeri') ->getTransport(); $transport->attachFile($path,'asd'); $transport->sendMessage(); with $transport =$transportBuilder->setTemplateIdentifier($templateId) ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId]) ->setTemplateVars($templateParams) ->setFrom(array('email'=>'anand@gmail.com', 'name'=>'anand')) ->addTo('anand.ontigeri@alshaya.com','anand.ontigeri') ->attachFile($path,'asd') //Add attachFile here ->getTransport(); $transport->sendMessage(); Related TopicMagento 2.3 – Custom Module Send Email Programmatically with Image Attachment
Best Answer
M2 does not come with out of the box however it is a feature built into the zend framework. Here is a good reference how to add this functionality into magento: https://blog.bitexpert.de/blog/sending-mails-with-attachments-in-magento-2/
In case link goes dead, create the following
then add to etc/di.xml
Now you can use
addAttachment()
throughout your site.