Let’s initiate a discussion!!
If you want to implement “add file attachment in the email” functionality in Magento 2, then please follow the below step.
Step 1) Create Vendor/Module/etc/di.xml
1 2 3 4 |
<?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="Vendor\Module\Model\Mail\TransportBuilder" /> </config> |
Step 2) Please create the email template in Vendor/Module/view/frontend/email/email_template.html
then after calling this file to Vendor/Module/etc/email_templates.xml
Step 3) CreateTransportBuilder and extend with \Magento\Framework\Mail\Template\TransportBuilder
Create Vendor/Module/Model/Mail/TransportBuilder.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php namespace Vendor\Module\Model\Mail; use Zend_Mime; 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; } } |
Step 4) Send mail using controller in execute method
Create Vendor/Module/Controller/Index/Index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
<?php namespace Vendor\Module\Controller\Index; use Magento\Framework\App\Action\Context; use Magento\Framework\UrlFactory; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Translate\Inline\StateInterface; use Vendor\Module\Model\Mail\TransportBuilder; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Store\Model\ScopeInterface; use Zend_Mime; class Index extends \Magento\Customer\Controller\AbstractAccount { public function __construct(Context $context, \Magento\Framework\Filesystem $fileSystem, StateInterface $inlineTranslation, TransportBuilder $transportBuilder, ScopeConfigInterface $scopeConfig, UrlFactory $urlFactory, \Magento\Framework\Message\ManagerInterface $messageManager ) { $this->_filesystem = $fileSystem; $this->urlModel = $urlFactory->create (); $this->inlineTranslation = $inlineTranslation; $this->_transportBuilder = $transportBuilder; $this->scopeConfig = $scopeConfig; $this->messageManager = $messageManager; parent::__construct ( $context ); } public function execute() { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $storeurl = $objectManager->get( 'Magento\Store\Model\StoreManagerInterface' )->getStore()->getBaseUrl(); $result = array(); $resultRedirect = $this->resultRedirectFactory->create (); if($_FILES['fileattach']['name']){ try { $uploader = $objectManager->create('Magento\MediaStorage\Model\File\Uploader',['fileId' => 'fileattach']); $uploader->setAllowedExtensions(['docx']); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); // get media directory $mediaDirectory = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA); // save the image to media directory $result = $uploader->save($mediaDirectory->getAbsolutePath()."fileupload"); $email = $this->scopeConfig->getValue('trans_email/ident_general/email',ScopeInterface::SCOPE_STORE); $name = $this->scopeConfig->getValue('trans_email/ident_general/name',ScopeInterface::SCOPE_STORE); $pdfFile = $result['path'].$result['file']; $emailTemplateVariables['message'] = 'This is a test message.'; $this->inlineTranslation->suspend(); $this->_transportBuilder->setTemplateIdentifier('email_template') // Add your template name ->setTemplateOptions( [ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $objectManager->get( 'Magento\Store\Model\StoreManagerInterface' )->getStore()->getId(), ] ) ->setTemplateVars($emailTemplateVariables) ->setFrom([ 'name' => 'From Name', 'email' => "test1@gmail.com", ]) ->addTo("test123@gmail.com", "To Name") ->addAttachment(file_get_contents($pdfFile),$result['type'],Zend_Mime::DISPOSITION_ATTACHMENT,Zend_Mime::ENCODING_BASE64,$result['name']); //Attachment goes here. try { $transport = $this->_transportBuilder->getTransport(); $transport->sendMessage(); $this->inlineTranslation->resume(); } catch (\Exception $e) { echo $e->getMessage(); die; } } catch (Exception $e) { \Zend_Debug::dump($e->getMessage()); } }else{ $url = $this->urlModel->getUrl ( '*/*/create', [ '_secure' => true ] ); $resultRedirect->setUrl ( $this->_redirect->error ( $url ) ); $this->messageManager->addError(__('Please upload the document')); return $resultRedirect; } } } |
[crayon-6421668d3325c454363819/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.