Learn how to send Magento 2 email and to send email easily with the platform in a few steps.
The following steps will send an Email in Magento 2. Listed are:
- email_templates.xml file
- email_templates.html
- Config.xml
- Manager files to send mail
Build your Amazon-like multi-vendor marketplace website @ $249/- now!!!
Get your free live demo now: Click here
Magento 2 Email: A Step-By-Step Procedure To Send Easily
Step 1: Email_templates.xml File
To create an email_templates.xml file in
app/code/local/[Name_Space]/[Your_Module]/etc
<?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="email_template.html" type="text" module="[Name_Space]_[Your_Module]" area="frontend"/> </config>
Step 2: Email_templates.html file
To create a Email_templates.html file in app/code/local/[Name_Space]/[Your_Module]/view/frontend/mail
<!--@subject Test Form@--> <!--@vars { "var data.comment":"Test", "var data.email":"Sender Email", "var data.name":"Sender Name" } @--> {{trans "Name: %name" name=$data.name}} {{trans "Email: %email" email=$data.email}} {{trans "Test: %test" test=$data.test}}
Step 3: Config.xml file
To create a config.xml file in
app/code/local/[Name_Space]/[Your_Module]/etc
<?xml version="1.0"?> <!-- /** * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> <default> <test> <email> <send_email>send_email_email_template</send_email> </email> </test> </default> </config>
Controller files to send Magento 2 email
To create a controller file to send email in app/code/local/[Name_Space]/[Your_Module]/controller/sendEmail
The following code:
/** * Recipient email config path */ const XML_PATH_EMAIL_RECIPIENT = 'test/email/send_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; } }
These are the following steps that will help you to send an email in Magento 2. I hope that this information will be helpful to you to send an Email.
Contact us to develop an eCommerce business website with 300+ features today!
Leave a Reply