I am trying to send emails from my custom module. But its not working.
1.) Created Email template with following path,
app/code/module/namespace/view/frontend/email/myemail.phtml
<!--@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>
</td></tr></table>
{{template config_path="design/email/footer_template"}}
2.) Declared email template in xml file.
app/code/module/namespace/etc/email_templates.xml
<?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"/>
</config>
3.) In my controller file,
public function execute()
{
$post = $this->getRequest()->getPostValue();
$this->inlineTranslation->suspend();
$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
$postObject->setData($post);
$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])
->setFrom($sender)
->addTo($senderToInfo)
->addBcc($senderBcc)
->getTransport();
$transport->sendMessage();
$this->inlineTranslation->resume();
$this->messageManager->addSuccess(__('Thanks'));
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setRefererOrBaseUrl();
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setRefererOrBaseUrl();
return $resultRedirect;
} catch (\Exception $e) {
$this->inlineTranslation->resume();
$this->messageManager->addError(__('Try again'));
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setRefererOrBaseUrl();
return $resultRedirect;
}
}