0

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???

<?php 
if(isset($_POST['submit'])){
$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($to,$subject,$message,$headers);
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.
}
?>
Teja Bhagavan Kollepara
  • 3,816
  • 5
  • 32
  • 69
sam
  • 489
  • 7
  • 23

1 Answers1

0

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(){
$html="
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 {
$mail->send();
Mage::getSingleton('core/session')->addSuccess('Your request has been sent');
$this->_redirect('');
}
catch (Exception $e) {
Mage::getSingleton('core/session')->addError('Unable to send.');
$this->_redirect('');
}
}

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

Pavan Kumar
  • 435
  • 1
  • 4
  • 13
  • This is the question related to magneto 2.x. in 2.x also, the code structure is same??? – sam Jul 06 '17 at 09: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 06 '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