I am using Magento-2.1.5, and I want to auto-generated mail as in response to the customer after successfully submitting the contact us form.

Can anyone please tell how to do that

if I will use this code in Phtml file, will it work: or I have to do something else???

$to = "email"; // this is cuatomer Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.

You can do it in 2 ways.

1. Create custom event to fire once the contact form has been submitted.
2. Create another mail action in the same controller and call the mail action in the try block.

public function sendMailAction(){
put your html content here
blah blah

$mail = Mage::getModel('core/email');
$mail->setToName('Your Name');
$mail->setToEmail('Youe Email');
$mail->setBody('Mail Text / Mail Content');
$mail->setSubject('Mail Subject');
$mail->setFromEmail('Sender Mail Id');
$mail->setFromName("Msg to Show on Subject");
$mail->setType('html');// YOu can use Html or text as Mail format

try {
Mage::getSingleton('core/session')->addSuccess('Your request has been sent');
catch (Exception $e) {
Mage::getSingleton('core/session')->addError('Unable to send.');

Both the ways leads to the same output. It depends on your choice.

  • This is the question related to magneto 2.x. in 2.x also, the code structure is same??? – sam Jul 6 '17 at 9:54
  • oh... sorry.. i thought this question is for 1.x, but you can use same kind of logic even in magento2 but declaration of event and controller is bit different – Pavan Kumar Jul 6 '17 at 10:00
  • can you please tell me how to chage this code in magento 2 form???? – sam Jul 10 '17 at 11:11

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.