1

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

Here is my controller file:

<?php

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
    ) {
        parent::__construct($context);
        $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();
        $postObject->setData($post);             
        /*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))
            ->setTemplateOptions(
                [
                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                    'store' => $this->storeManager->getStore()->getId(),
                ]
            )
            ->setTemplateVars(['data' => $postObject])
            ->setFrom($senderInfo)
            ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
            ->getTransport();


            $transport->sendMessage();


          /*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-->
<table>
    <tr class="email-intro">
        <td>
            <p class="greeting">Hi,</p>

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

        </td>
    </tr>
</table>

{{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

2
  • 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
0

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();
2
0

You have to write below line in your email html,

{{trans "Email: %email" email=$data.email}}
1
  • 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.