I am trying to send emails from my custom module. But its not working.

1.) Created Email template with following path,


<!--@subject MY EMAIL SUBJECT HERE @-->
{{template config_path="design/email/header_template"}} 
<table><tr class="email-intro"><td>
 <p class="greeting">{{trans "%myname," customer_name=$data.myname}}</p>
 <p class="greeting">{{trans "%myemail," customer_email=$data.myemail}}</p>
{{template config_path="design/email/footer_template"}}

2.) Declared email template in xml file.


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Email/etc/email_templates.xsd">
    <template id="mymodule_email_template" label="Email Form" file="myemail.html" type="html" module="Jute_Ecommerce" area="frontend"/>

3.) In my controller file,

public function execute()
    $post = $this->getRequest()->getPostValue(); 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $customerSession = $objectManager->create('Magento\Customer\Model\Session');

    try {
        $postObject = new \Magento\Framework\DataObject();
        $post['myname'] = $customerSession->getCustomer()->getName(); //Loggedin customer Name
        $post['myemail'] = $customerSession->getCustomer()->getEmail(); 

        //Loggedin customer Email

        $myname = $post['myname'];
        $myemail = $post['myemail'];

        $sender = [
            'name' => $this->_escaper->escapeHtml($myname),
            'email' => $this->_escaper->escapeHtml($myemail),

        $sentToEmail = $this->scopeConfig->getValue('trans_email/ident_support/email',ScopeInterface::SCOPE_STORE);
        $sentToname = $this->scopeConfig->getValue('trans_email/ident_support/name',ScopeInterface::SCOPE_STORE);

        $senderToInfo = [
            'name' => $this->_escaper->escapeHtml($sentToname),
            'email' => $this->_escaper->escapeHtml($sentToEmail),

        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE; 
        $transport = $this->_transportBuilder
            ->setTemplateIdentifier('mymodule_email_template') // My email template
            ->setTemplateOptions( [
                'area' => \Magento\Framework\App\Area::AREA_FRONTEND, // this is using frontend area to get the template file if admin then \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE
                'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
            ->setTemplateVars(['data' => $postObject])



        $resultRedirect = $this->resultRedirectFactory->create();
        $resultRedirect = $this->resultRedirectFactory->create();

        return $resultRedirect;
    } catch (\Exception $e) {
        $this->messageManager->addError(__('Try again'));

        $resultRedirect = $this->resultRedirectFactory->create();
        return $resultRedirect;
  • Have yo test your email is working or not for order and other default feature? – Rakesh Jesadiya Oct 26 '16 at 12:33
  • Email is working for order as well as customer registration process. But Email is not working for custom module. – Murali Raj Oct 26 '16 at 12:34
  • Which error are display in log file? – Rakesh Jesadiya Oct 26 '16 at 12:37
  • There is no error related to emails in log file. – Murali Raj Oct 26 '16 at 12:44

I am used the following code to send email in my custom extension. And it working fine.

namespace [Vender]\[Extension]\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\Framework\App\RequestInterface;
use \Magento\Framework\Mail\Template\TransportBuilder;
use \Magento\Framework\Translate\Inline\StateInterface;
use Psr\Log\LoggerInterface;

class Sendmail extends \Magento\Framework\App\Action\Action
    const XML_PATH_EMAIL_ADMIN_QUOTE_SENDER = 'Email Sender';
    const XML_PATH_EMAIL_ADMIN_NAME = 'Sender Name';
    const XML_PATH_EMAIL_ADMIN_EMAIL = 'Receiver Email';

    protected $inlineTranslation;
    protected $transportBuilder;
    protected $_logLoggerInterface;

    public function __construct(
    Context $context,
    StateInterface $inlineTranslation,
    TransportBuilder $transportBuilder,
    LoggerInterface $logLoggerInterface,
    array $data = [])
        $this->inlineTranslation = $inlineTranslation;
        $this->transportBuilder = $transportBuilder;
        $this->_logLoggerInterface = $logLoggerInterface;

    public function execute()

            // Send Mail
            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $transport = $this->transportBuilder
               ->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_ADMIN_QUOTE_NOTIFICATION, $storeScope))
                        'area' => 'frontend',
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    'var1'  => 'Value',
                    'var2'  => 'Value'
               ->setFrom($this->scopeConfig->getValue(self::XML_PATH_EMAIL_ADMIN_QUOTE_SENDER, $storeScope))
               ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_ADMIN_EMAIL, $storeScope))
        } catch(\Exception $e){
  • i have tried your code. but scopeconfig is undefined error is coming. – Murali Raj Nov 2 '16 at 6:15
  • Hi, Dhiren. I have used your code and its working fine. Now I am trying to use the same code in another module, but its not working. – Murali Raj Nov 4 '16 at 9:36
  • Is that any error show when you used in another place? And above andwer help you, accept as solution so other can also get help. – Dhiren Vasoya Nov 7 '16 at 4:34
  • When I am including construct function I am getting blank white screen(500 internal server error). When I removed the construct function code, then the code is working. I think construct function is problem. But I used the same construct function in other module.its working. – Murali Raj Nov 7 '16 at 5:43
  • 1
    Hi @Dhiren Vasoya, let me know how to preview the email before send in front end site – kamal Feb 22 '18 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.