Magento – How to send an Email confirmation after contact us

contact-formemailmagento2

When the client uses the contact form, how can I get a confirmation email to the user?

Best Answer

I think your need to send some Email confirmation to the user when he/she send a contact form !

Lest's go, we will use an observer for that with controller_action_postdispatch_contact_index_post as event.

In your module you have to add these 4 files, you replace {Vendor}/{Module} with you vendor, module name.

app/code/{Vendor}/{Module}/Observer/SendContactConfirmationEmail.php

app/code/{Vendor}/{Module}/etc/events.xml

app/code/{Vendor}/{Module}/etc/email_templates.xml

app/code/{Vendor}/{Module}/view/frontend/email/email_template.html

A Content :

app/code/{Vendor}/{Module}/Observer/SendContactConfirmationEmail.php

<?php
/**
 * Class SendContactConfirmationEmail
 * @object Send an email confirmation to the user after submi a contact form
 * @autor @mir
 */

namespace {Vendor}\{Modulename}\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class SendContactConfirmationEmail implements ObserverInterface
{
    /**
     * @var \Magento\Framework\App\Request\Http
     */
    protected $_request;

    /**
     * @var \Magento\Framework\Mail\Template\TransportBuilder
     */
    protected $_transportBuilder;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * For logger, var/log
     */
    protected $_logger;



    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->_request = $request;
        $this->_transportBuilder = $transportBuilder;
        $this->_storeManager = $storeManager;
        $this->_logger = $logger;
    }


    public function execute(EventObserver  $observer)
    {
        $event = $observer->getEvent();
        $name = $event->getRequest()->getPost()['name'];
        $email = $event->getRequest()->getPost()['email'];
        $content = $event->getRequest()->getPost()['comment'];

        if ($email) {
            try {
                $store = $this->_storeManager->getStore()->getId();
                $transport = $this->_transportBuilder->setTemplateIdentifier('contact_confirmation_template')
                    ->setTemplateOptions(['area' => 'frontend', 'store' => $store])
                    ->setTemplateVars(
                        [
                            'store' => $this->_storeManager->getStore(),
                            'name' => $name,
                            'email' => $email,
                            'content' => $content,
                        ]
                    )
                    ->setFrom('general') //Store -> Configuration -> General -> Store Email Addresses
                    ->addTo($email, $name)
                    ->getTransport();
                $transport->sendMessage();
                //$this->_logger->info("Message sent !");
                return;
            } catch (\Exception $e) {
                $this->_logger->critical($e->getMessage());
                return;
            }
        }
    }
}

app/code/{Vendor}/{Module}/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_postdispatch_contact_index_post">
        <observer name="sendEmailCustomerContactConfirmation" instance="\Sd\Sdsolutions\Observer\SendContactConfirmationEmail" />
    </event>
</config>

app/code/{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="contact_confirmation_template" label="Confirmation contact email" file="email_template.html" type="html" module="Vendor_Modulename" area="frontend"/>
</config>

app/code/{Vendor}/{Module}/view/frontend/email/email_template.html

<!--@subject Contact Form Confirmation@-->

<p>Dear <b>{{var name}}</b>, your message has been received, we will answer you in the best time.</p>

<p>Your message:</p>
<h4>{{var content}}</h4>

<p>Regards.</p>

Good luck.

Related Topic