When the client uses the contact form, how can I get a confirmation email to the user?
4 Answers
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 Oct 29, 2018 at 12:31
-
-
HI @Prince.. I tried your solution. It is sending mails but issue is the customer receives mail but also the admin/client email where the contact form data mail is send also receives thankyou mail– ArjunApr 28, 2020 at 13:17
-
Sending back a copy of the comment content is really a bad idea. What is to stop anyone entering any email address in and spam content. Apr 28, 2020 at 18:41
- On the Admin sidebar, tap Stores. Then under Settings, choose Configuration.
- In the panel on the left under General, choose Contacts
- Expand the Email Options section. Then, do the following:
- In the Send Emails to field, enter the email address where messages from the Contact Us form are sent.
- 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.
- Set Email Template to the template that is used for messages sent from the Contact Us form.
- Save
I put together a solution using plugin
https://magento.stackexchange.com/a/311559/70343
Here is link to repo
https://github.com/DominicWatts/ContactResponse
It's always safer to go plugin route as less likely to conflict with other extensions
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 -
confirmation email to the user
not for himself ! there no configuration for that