Autoresponder for Magento 2

How to create an Autoresponder email for Magento Contact Form

In Magento 2 by default, we don’t have an option for autoresponse for contact form In this article I have explained step by step guide to create an auto response for the contact form.

Note: you should have to know the basics of Magento 2 module because we directly jump into overriding the Magento_Contact controller.

Step1:

Create di: Vendor\Module\etc\di.xml

<?xml version="1.0"?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <preference for="Magento\Contact\Controller\Index\Post" type="Vendor\Module\Controller\Rewrite\Contact\Post" />
 </config>

Step2:

Create your controller as per di.xml preference type :Vendor\Module\Controller\Rewrite\Contact\Post

namespace Vendor\Module\Controller\Rewrite\Contact;

use Magento\Framework\App\RequestInterface;
 use Magento\Framework\Mail\Template\TransportBuilder;
 use Magento\Framework\Translate\Inline\StateInterface;

class Post extends \Magento\Contact\Controller\Index\Post
 {
 /**
 * @var \Magento\Framework\App\Request\Http
 */
 protected $_request;
 /**
 * @var TransportBuilder
 */
 protected $_transportBuilder;
 /**
 * @var StateInterface
 */
 protected $_inlineTranslation;
 /**
 * @var \Magento\Framework\App\Config\ScopeConfigInterface
 */
 protected $_scopeConfig;
 /**
 * @var \Magento\Store\Model\StoreManagerInterface
 */
 protected $_storeManager;

/**
 * @param \Magento\Framework\App\Action\Context $context
 * @param TransportBuilder $transportBuilder
 * @param StateInterface $inlineTranslation
 * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
 * @param \Magento\Store\Model\StoreManagerInterface $storeManager
 */

public function __construct(
 \Magento\Framework\App\Action\Context $context,
 TransportBuilder $transportBuilder,
 StateInterface $inlineTranslation,
 \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
 \Magento\Store\Model\StoreManagerInterface $storeManager,
 \Magento\Framework\App\Request\Http $request
 )
 {
 parent::__construct($context,$transportBuilder,$inlineTranslation,$scopeConfig,$storeManager);
 $this->_transportBuilder = $transportBuilder;
 $this->_inlineTranslation = $inlineTranslation;
 $this->_scopeConfig = $scopeConfig;
 $this->_storeManager = $storeManager;
 $this->_request = $request;
 }

public function execute()
 {
 $post = $this->getRequest()->getPostValue();
// we're getting the name & email and passing it to sendMailAction() to send autoresponse email
if (\Zend_Validate::is(trim($post['name']), 'NotEmpty') && \Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
 $this->sendMailAction($post['name'],$post['email']);
 }
 return parent::execute();
 }

public function sendMailAction($customerName,$customerEmail){ 
$store = $this->_storeManager->getStore()->getId();
 

$transport = $this->_transportBuilder->setTemplateIdentifier('modulename_template') // setting email template id
 ->setTemplateOptions(['area' => 'frontend', 'store' => $store])
 ->setTemplateVars(
 [
 'store' => $this->_storeManager->getStore(),
 ]
 )
 ->setFrom('general')
 ->addTo($customerEmail, $customerName)
 ->getTransport();
 $transport->sendMessage();
 return $this;

}
 }

Step3:

Create email_templates.xml in Vendor\Module\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="modulename_template" label="Autoresponder email" file="autoresponder.html" type="html" module="Vendor_Module" area="frontend"/>

Step4:

Finally, Create the email template inside view: Vendor\Module\view\frontend\email\autoresponder.html

<!--@subject Your Email subject Goes Here @-->
Your Email Message here

Hope you enjoyed this tutorial about sending auto-responding email to the contact form in Magento 2, Help us improve by sharing this page and comment your suggestion

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.