简体   繁体   中英

php mail From: & Reply-to: headers issue

Ok so I am writing a php contact form that will send two emails. One to the webmaster with the info and a similar confirmation email sent to the form submitter.

The problem is with the confirmation email. I am trying to configure the From: header and the Reply-to: but and for some reason catching a hurdle.

Originally I had my php set up as so declaring each header parameter...

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$guests = $_POST['guests'];
$type = $_POST['type'];
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
$message = $_POST['message'];
$formcontent="THIS IS A FORM SUBMISSION FROM domain.COM... \n \n PARTY INQUIRY \n \n From: $name \n Email: $email \n Phone: $phone \n # of Guests: $guests \n Type: $type \n Date Requested: $month $day, $year \n \n Additional Info: $message";
$comfirmcontent="THIS IS A CONFIRMATION OF YOUR FORM SUBMISSION TO domain.COM... \n \n PARTY INQUIRY \n \n From: $name \n Email: $email \n Phone: $phone \n # of Guests: $guests \n Type: $type \n Date Requested: $month $day, $year \n \n Additional Info: $message \n\n\n If you have any further questions please email info@mydomain.com";
$confirmsubject="Confirmation for your inquiry to mydomain.COM";
$confirmheader="From: mydomain.com" . "\r\n" .
"Reply-To: info@mydomain.com" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$recipient = "info@domain.com";
$subject = "Party Inquiry from Website";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
mail($email, $confirmsubject, $comfirmcontent, $confirmheader) or die("Error!");
header('Location: party-form-thank-you.html')

This as I understand is the proper way, but I am still having the From: say "mydomain.com@myhostdomain.com as both the senders name and senders email. Which is how it was before I even declared the headers and it was null.

So then I tried an override using

mail($email, $confirmsubject, $comfirmcontent, $confirmheader,'-finfo@mydomain.com') or die("Error!");

Which returned the same results.

So I have resorted to simply using the following, w/o declared headers.

mail($email, $confirmsubject, $comfirmcontent, null,'-finfo@mydomain.com') or die("Error!");

This give me the proper from/reply-to address, but also puts it as the senders name.

So my question is if there is any way to write the headers so the email formulates as such:

Senders Name: myDomain.com

Senders Email: info@mydomain.com

I use an array for my headers, then implode them on "\\r\\n":

$headers = array();
$headers[] = 'Content-type: text/html; charset="UTF-8";';
$headers[] = 'Date: ' . date('r', $_SERVER['REQUEST_TIME']);
$headers[] = 'Message-ID: <' . $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . (empty($_SERVER['SERVER_NAME']) ? '' : '@' . $_SERVER['SERVER_NAME']) . '>';

// here is the from and reply-to part:
$headers[] = 'From: "' . $fromName . '" <' . $fromAddress . '>';
$headers[] = 'Reply-To: "' . $replyToName . '" <' . $replyToAddress . '>';
$headers[] = 'X-Mailer: PHP v' . phpversion();
if (!empty($_SERVER['SERVER_ADDR'])) {
    $headers[] = 'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'];
}

// Use the implode function to collapse the array into a single string
mail($to, $subject, $message, implode("\r\n", $headers));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM