0

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

  • He made it clear confirmation email to the user not for himself ! there no configuration for that – PЯINCƏ Oct 26 '18 at 21:54
1

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.

  • I'm going to test your solution, I'm with version 2.2.3 of magento. ty bro – Alfredo López Nunes Oct 29 '18 at 12:31
  • @AlfredoLópezNunes did it work? – Alex Jul 1 at 13:48
0
  1. On the Admin sidebar, tap Stores. Then under Settings, choose Configuration.
  2. In the panel on the left under General, choose Contacts
  3. Expand the Email Options section. Then, do the following:
  4. In the Send Emails to field, enter the email address where messages from the Contact Us form are sent.
  5. Set Email Sender to the store identity that appears as the sender of the message from the Contact Us form. For example: Custom Email 2.
  6. Set Email Template to the template that is used for messages sent from the Contact Us form.
  7. Save
-1

To get contact configuration navigate to

Store > Configuration > General > Contacts

and set email in field Send Emails to where you want to get contact form email.

Check image for help -

enter image description here

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.