0

I have created custom module and configured the custom email template when submitting form, and i am triggering custom email when submitting form.

After receiving email, displaying luma logo

Can you please advise?

I have done the all the settings at admin end. but no luck

app/code/Demo/Test/Controller/Index;

 <?php

namespace Demo\Test\Controller\Index;
use Magento\Contact\Model\ConfigInterface;
use Magento\Contact\Model\MailInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\HTTP\PhpEnvironment\Request;
class Post extends \Magento\Framework\App\Action\Action
{

        /**
        * Recipient email config path
        */
        const XML_PATH_EMAIL_TEMPLATE = 'contactus/email/send_email';
        const XML_PATH_EMAIL_RECIPIENT = 'contact/email/recipient_email';
        /**
        * @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;
        /**
        * @var \Magento\Framework\Escaper
        */
        protected $_escaper;

        /**
        * @var MailInterface
        */
        private $mail;

        /**
        * @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\Escaper $escaper,
        MailInterface $mail
        ) {
        parent::__construct($context);
        $this->_transportBuilder = $transportBuilder;
        $this->inlineTranslation = $inlineTranslation;
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
        $this->_escaper = $escaper;
        $this->mail = $mail;
        }

    /**
    * Post user question
    *
    * @return void
    * @throws \Exception
    */
        public function execute()
        {
            $post = $this->getRequest()->getPostValue();
            if (!$post) {
            $this->_redirect('*/*/');
            return;
        }

        $this->inlineTranslation->suspend();
        try {
            $postObject = new \Magento\Framework\DataObject();
            $postObject->setData($post);

            $error = false;

            if (!\Zend_Validate::is(trim($post['name']), 'NotEmpty')) {
                $error = true;
            }
            if (!\Zend_Validate::is(trim($post['comment']), 'NotEmpty')) {
                $error = true;
            }
            if (!\Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                $error = true;
            }
            if (\Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                $error = true;
            }
            if ($error) {
                throw new \Exception();
            }
            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $transport = $this->_transportBuilder
                ->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_TEMPLATE, $storeScope))
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ]
                )
                ->setTemplateVars(['data' => $postObject])
                ->setFrom('general')              
                ->setReplyTo($post['email'],$post['name'])
                ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
                ->getTransport();

            $transport->sendMessage();
            $this->inlineTranslation->resume();
            $this->messageManager->addSuccess(
                __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')
            );
            $this->_redirect('*/*/');
            return;
        } catch (\Exception $e) {
            $this->inlineTranslation->resume();
            $this->messageManager->addError(
                __('We can\'t process your request right now. Sorry, that\'s all we know.')
            );
            $this->_redirect('*/*/'); 
            return;
        }


    }
}

app/code/Demo/Test/etc/email_templates.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
<template id="contactus_email_email_template" label="International Shipping" file="submitted_form.html" type="html" module="Demo_Test" area="frontend"/>
</config>

app/code/Demo/Test/etc/config.xml

   <?xml version="1.0"?>
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
  <default>
    <contactus>
     <email>
         <send_email>contactus_email_email_template</send_email>
     </email>
    </contactus>
</default>

app/code/Demo/Test/view/frontend/email/submitted_form.html

<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<!--@subject {{trans "Regarding International Customs Duties"}} @-->
<!--@vars {
"var data.comment":"Comment",
"var data.email":"Sender Email",
"var data.name":"Sender Name",
"var data.telephone":"Sender Telephone"
} @-->

{{template config_path="design/email/header_template"}}

<table class="message-details">
    <tr>
        <td><b>{{trans "Name"}}</b></td>
        <td>{{var data.name}}</td>
    </tr>
    <tr>
        <td><b>{{trans "Email"}}</b></td>
        <td>{{var data.email}}</td>
    </tr>
    <tr>
        <td><b>{{trans "Phone"}}</b></td>
        <td>{{var data.telephone}}</td>
    </tr>
</table>
<p><b>{{trans "Message"}}</b></p>
<p>{{var data.comment}}</p>

{{template config_path="design/email/footer_template"}}

enter image description here

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.