2

I have used the below code for a custom module to send mail. Its working fine. I want to send email for another custom module. So I duplicated the same controller file and changed the required parameters and modulename. But its not working. I dont know what to do. Please someone suggest me a good idea.

  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>
  1. app/code/module/namespace/Controller/Post

/**

namespace vendorname\modulename\Controller\Post;

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 Index extends \Magento\Framework\App\Action\Action{
/**
* Post user question
*
* @return void
* @throws \Exception
*/    

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

public function __construct(
Context $context,
StateInterface $inlineTranslation,
TransportBuilder $transportBuilder,
LoggerInterface $logLoggerInterface,
\vendorname\madulename\Helper\Data $helperData,
array $data = [])
{
    $this->inlineTranslation = $inlineTranslation;
    $this->transportBuilder = $transportBuilder;
    $this->_logLoggerInterface = $logLoggerInterface;
    $this->_helperData = $helperData;
    parent::__construct($context,$data);
}

public function execute(){
    $post = $this->getRequest()->getPostValue();
    if (!$post) {
        $this->_redirect('redirect_url');
        return;
    }
    //try {
        $postObject = new \Magento\Framework\DataObject();
        $postObject->setData($post);

        $error = false;

        if (!\Zend_Validate::is(trim($post['name']), 'NotEmpty')) {
            $error = true;
        }
        if ($error) {
        throw new \Exception();
        }

        $cname = $post['name'];
        $cemail = $post['email'];
        $cmobile = $post['phone'];
        $csubject = $post['subject'];

        $model = $this->_objectManager->create('vendorname\modulename\Model\Enquiry');

        $model->setName($name);
        $model->setEmail($email);
        $model->setPhonenumber($phone);
        $model->setSubject($subject);

        $model->setCreatedAt($date);
        $model->save();
        $toEmail = $this->_helperData->getConfig('trans_email/ident_support/email');

        $sender = [
         'name' => $cname,
         'email' => $cemail,
         ];


        $this->inlineTranslation->suspend();
        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $transport = $this->transportBuilder
           ->setTemplateIdentifier('mymodule_email_template')
           ->setTemplateOptions(
                [
                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                    'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                ]
            )
           ->setTemplateVars(['data' => $postObject])
           ->setFrom($sender)
           ->addTo($toEmail)
           ->getTransport();
        $transport->sendMessage();
        $this->inlineTranslation->resume();
        $this->_redirect('thank_you_redirect');
        return;
    /* } catch (\Exception $e) {            
        $this->messageManager->addError(
        __('We can\’t process your request right now. Sorry, that\’s all we know.')
    );
    } */ }  
}
  • Is it correct that in your xml you use: 'myemail.html' and as file you have 'phtml'? – 11mb Dec 2 '16 at 15:48
  • Yeah!! I used "myemail.phtml" only. – Murali Raj Dec 5 '16 at 6:57
  • Ok, but shouldn't you reference the file as .phtml in your xml as well? – 11mb Dec 5 '16 at 8:54
  • 1
    I have used as .phtml only. Thats typing mistake. Sorry!! – Murali Raj Dec 5 '16 at 10:14
  • Did you take load order into account? devdocs.magento.com/guides/v2.0/extension-dev-guide/build/… – 11mb Dec 7 '16 at 8:16

Your Answer

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

Browse other questions tagged or ask your own question.