I want to send emails from my custom module programatically. Email sending is working fine.

Here is my controller file:


namespace Company\Module\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
     * Recipient email config path

    const XML_PATH_EMAIL_RECIPIENT = 'company_module/general/lease_from_mail';
    const XML_PATH_EMAIL_TEMPLATE = 'company_module/general/template';

     * @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;

    protected $remoteAddress;

    protected $resultJsonFactory; 

     * @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\HTTP\PhpEnvironment\RemoteAddress $remoteAddress,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
    ) {
        $this->_transportBuilder = $transportBuilder;
        $this->inlineTranslation = $inlineTranslation;
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
        $this->remoteAddress = $remoteAddress;
        $this->resultJsonFactory = $resultJsonFactory;

    public function execute()

        $post = $this->getRequest()->getPostValue();
        //echo '<pre>';print_r($post);exit;
        $result = $this->resultJsonFactory->create();
        $postObject = new \Magento\Framework\DataObject();
        /*Email Sending Start*/

            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $senderInfo = [
            'name' =>  $this->scopeConfig->getValue('trans_email/ident_general/name', $storeScope),
            'email' => $this->scopeConfig->getValue('trans_email/ident_general/email', $storeScope)
            $transport = $this->_transportBuilder
            ->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_TEMPLATE, $storeScope))
                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                    'store' => $this->storeManager->getStore()->getId(),
            ->setTemplateVars(['data' => $postObject])
            ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))


          /*Email Sending End*/


This is my email template.

<!--@subject Lease Form  @-->
<!--@vars {
"var this.getUrl($store, 'admin')":"Warehouse Account URL",
"var customer.email":"Customer Email",
"var customer.name":"Customer Name"
} @-->

{{template config_path="design/email/header_template"}} <!-- pathe of template header-->

<!-- here $myvar1 , $myvar2, $myvar3, $myvar4, $myvar5, $myvar6 are variables in which we
                                     asssign values when we use this template for send mail-->

<!-- you can Modify content of template according to your requirement-->
    <tr class="email-intro">
            <p class="greeting">Hi,</p>

    <tr class="email-summary">
          <p>{{trans "Test test "}}</p> 
          <p><strong>Equipment Requested:- </strong> {{$data.email()}} </p>


{{template config_path="design/email/footer_template"}} <!--footer of template-->

I have passed the $postObject as data object. I want to get each variable in that object in the template. How is it possible?

Thanks in advance

  • try with just {{var data.email}} instead of {{$data.email()}} Apr 27 '17 at 11:32
  • please let me know if you have issue Apr 27 '17 at 11:56

If the content of $postObject is an associative array, for example something like this:

$postObject= [
    'email' => 'test@test.com'

Then you can do the following in your template:

{{var email}}

If your $postObject is an instance of Magento\Framework\DataObject then you'll have to convert it to an array first by calling:

$postObject = $postObject->toArray();

You have to write below line in your email html,

{{trans "Email: %email" email=$data.email}}
  • you can close this question after accept as answer. Apr 28 '17 at 5:21

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.