How to send Email in Magento 2

The following steps will send Email in Magento 2. Listed are:

  1. email_templates.xml file
  2. email_templates.html
  3. Config.xml
  4. Manager files to send mail

Step 1: Email_templates.xml File

To create a 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 mail

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 which will help you to send email in magento 2. I hope that this information will be helpful to you to send Email .Each store has an Email in magento 2 with several elements.

Last Update: April 6, 2018  

February 3, 2018   575   Nandini Ramachandran    Marketing    
Total 2 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
INSTAGRAM
LinkedIn