0

Magento2 I want to send a thankyou email to customer after filling up the Contact Us form.

In order to achieve this I used below link custom mode

How to send an Email confirmation after contact us

But I am facing a strange issue, as a customer, I received the thankyou email properly but the admin email id present under Store > Configuration > General > Contacts > Send Emails to also received the Thankyou email.

In short,

Email 1: Admin received Form data email(mentioned under Send Emails to)

Email 2: Customer received Thankyou mail

Email 3: Admin received Thankyou mail(mentioned under Send Emails to)

Email 3 is incorrect, this should not have being received.

Any suggestions will be appreciated. Thanks in advance.

1
  • Please be aware the reason this doesnt happen by default is because someone can enter any email address and your server will send the confirmation to that address. Therefore without captcha you will soon be sending a lot of spam. Definitely don't repeat back the content as shown in that other post. Commented Apr 28, 2020 at 18:37

1 Answer 1

1

I think you want something like this

https://github.com/DominicWatts/ContactResponse

Much of the hard work is done by the plugin

https://github.com/DominicWatts/ContactResponse/blob/48e9cb23f72990551d5c2dbb51605871024e53b2/Plugin/Frontend/Magento/Contact/Model/Mail.php#L72-L116

    public function afterSend(
        \Magento\Contact\Model\Mail $subject,
        $result,
        $variables,
        $replyTo
    ) {
        if (!$this->helper->getConfirmation()) {
            return;
        }


        /** @see \Magento\Contact\Controller\Index\Post::validatedParams() */
        $replyToName = !empty($replyTo['data']['name']) ? $replyTo['data']['name'] : null;
        $replyToEmail = !empty($replyTo['data']['email']) ? $replyTo['data']['email'] : null;


        // $this->contactsConfig->emailSender(); string
        // $this->contactsConfig->emailRecipient(); email

        $this->inlineTranslation->suspend();
        try {
            $transport = $this->transportBuilder
                ->setTemplateIdentifier($this->helper->getConfirmationTemplate())
                ->setTemplateOptions(
                    [
                        'area' => Area::AREA_FRONTEND,
                        'store' => $this->storeManager->getStore()->getId()
                    ]
                )
                ->setTemplateVars($replyTo)
                ->setFrom([
                    'email' => $this->contactsConfig->emailRecipient(),
                    'name' => $this->contactsConfig->emailSender()
                ])
                ->addTo($replyToEmail)
                ->setReplyTo(
                    $this->contactsConfig->emailRecipient(),
                    $this->contactsConfig->emailSender()
                )
                ->getTransport();


            $transport->sendMessage();
        } finally {
            $this->inlineTranslation->resume();
        }
        return $result;
    }

Email template markup is here

https://github.com/DominicWatts/ContactResponse/blob/master/view/frontend/email/response.html

<!--@subject {{trans "Thanks for getting in touch"}} @-->
<!--@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>Thanks for reaching out to us {{var data.name}}</strong></td>
    </tr>
    <tr>
        <td>One of the team will be in touch soon.</td>
    </tr>
</table>

{{template config_path="design/email/footer_template"}}
2
  • Hi, The module is working fine but I am facing an issue that I have already used this <type name="Magento\Contact\Controller\Index\Post"> <plugin name="custom_contact_action" type="Vendor\ContactAutoRespond\Plugin\Post"/> </type> & so the mail doesn't get send. If I remove above code then it is working fine. I want controller plugin & Model Plugin to be used together
    – Arjun
    Commented Apr 29, 2020 at 6:18
  • You are going to have to merge the logic from both extensions into a single extension and possibly take one out. It's difficult but it happens. Commented Apr 29, 2020 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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