In Magento 2 we can send the mail using their function but there are some changes in Magento 2.
Now Let’s start how to send the email.
1. create email_templates.xml in the path app/code/Webmull/Email/etc
1 2 3 4 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Email/etc/email_templates.xsd"> <template id="send_email_email_template" label="Email Form" file="send_form.html" type="text" module="Webmull_Email" area="frontend"/> </config> |
2. create send_form.html in the path app/code/Webmull/Email/view/frontend/email
1 2 3 4 5 6 7 8 9 10 11 |
<!--@subject Contact Form@--> <!--@vars { "var data.comment":"Comment", "var data.email":"Sender Email", "var data.name":"Sender Name" } @--> {{trans "Name: %name" name=$data.name}} {{trans "Email: %email" email=$data.email}} {{trans "Comment: %comment" comment=$data.comment}} |
 3. Write the code in controller file to send the email
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
/** * Recipient email config path */ const XML_PATH_EMAIL_RECIPIENT = 'contact/email/recipient_email'; /** * @var \Magento\Framework\Mail\Template\TransportBuilder */ protected $_transportBuilder; /** * @var \Magento\Framework\Translate\Inline\StateInterface */ protected $inlineTranslation; /** * @var \Magento\Framework\App\Config\ScopeConfigInterface */ protected $scopeConfig; /** * @var \Magento\Store\Model\StoreManagerInterface */ protected $storeManager; /** * @var \Magento\Framework\Escaper */ protected $_escaper; /** * @param \Magento\Framework\App\Action\Context $context * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig * @param \Magento\Store\Model\StoreManagerInterface $storeManager */ public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder, \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Escaper $escaper ) { parent::__construct($context); $this->_transportBuilder = $transportBuilder; $this->inlineTranslation = $inlineTranslation; $this->scopeConfig = $scopeConfig; $this->storeManager = $storeManager; $this->_escaper = $escaper; } /** * Post user question * * @return void * @throws \Exception */ public function execute() { $post = $this->getRequest()->getPostValue(); if (!$post) { $this->_redirect('*/*/'); return; } $this->inlineTranslation->suspend(); try { $postObject = new \Magento\Framework\DataObject(); $postObject->setData($post); $error = false; $sender = [ 'name' => $this->_escaper->escapeHtml($post['name']), 'email' => $this->_escaper->escapeHtml($post['email']), ]; $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE; $transport = $this->_transportBuilder ->setTemplateIdentifier('send_email_email_template') // this code we have mentioned in the email_templates.xml ->setTemplateOptions( [ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, // this is using frontend area to get the template file 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, ] ) ->setTemplateVars(['data' => $postObject]) ->setFrom($sender) ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope)) ->getTransport(); $transport->sendMessage(); ; $this->inlineTranslation->resume(); $this->messageManager->addSuccess( __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.') ); $this->_redirect('*/*/'); return; } catch (\Exception $e) { $this->inlineTranslation->resume(); $this->messageManager->addError( __('We can\'t process your request right now. Sorry, that\'s all we know.'.$e->getMessage()) ); $this->_redirect('*/*/'); return; } } |
If we want to use the email of frontend write this area
\Magento\Framework\App\Area::AREA_FRONTEND
if we want to use the email of backend area
\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE
Now check the email 🙂