0

Would like to send email to the person who filled out the form and one to the admin of the website to respond back to the client. The code sends properly to the person who filled the form, but nothing to the admin. See code below.

<?php

$EmailFrom = "jason@mysite.com";
$EmailTo = stripslashes($_POST['email']);
$Subject = stripslashes($_POST['subject']);
$Name = Trim(stripslashes($_POST['author'])); 
$Email = Trim(stripslashes($_POST['email']));
$Message = Trim(stripslashes($_POST['message'])); 



// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = '';
$Body .= "Thank you for contacting us! One of our staff will contact you soon.";
$Body .= "\n";
$Body .= "\n";
$Body .= "";
$Body .= "\n";
$Body .= "\n";
$Body .= "----------------------------";
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $Subject;
$Body .= "\n";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

ini_set($EmailFrom, $EmailTo);
// send email 
$success = mail($EmailTo, $Subject, $Body, 'From: ' . $EmailFrom);

// redirect to success page 
if ($success){
  echo "Thank you for contacting us!";
}
else{
  echo "Please Try Again.";
}
?>
Jason
  • 1

1 Answers1

0

This isn't doing anything useful:

ini_set($EmailFrom, $EmailTo);

so don't call that. If you want to send two messages (as opposed to 2 copies of the same message), call mail() again with a different to address (assuming that your $EmailFrom is the admin's address):

$success = mail($EmailFrom, $Subject, $Body, 'From: ' . $EmailFrom);

That said, you're probably better off looking at PHPMailer that you tagged this question with as it's much easier to send to multiple recipients safely with that.

Synchro
  • 29,823
  • 14
  • 69
  • 85
  • Thank you for the help. I am new to this and trying to figure it all out. – Jason Apr 21 '20 at 20:37
  • When you say call mail() again. Do you mean to duplicate this.. $EmailFrom = "jason@mysite.com"; $EmailTo = stripslashes($_POST['email']); $Subject = stripslashes($_POST['subject']); $Name = Trim(stripslashes($_POST['author'])); $Email = Trim(stripslashes($_POST['email'])); $Message = Trim(stripslashes($_POST['message'])); – Jason Apr 21 '20 at 20:46
  • Would this be the correct way? – Jason Apr 21 '20 at 21:39
  • No, all I suggested was to call mail() again with a different recipient. You don’t need to touch the rest of the data. – Synchro Apr 22 '20 at 07:09