Formatting email headers

Hi All,

Im new to the forum. Ive been trying to write a simple script that will process a form from my website. I have the form working and everything, here is my code:

[php]
$headers = ‘MIME-Version: 1.0’ . “\r\n”;

$headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “\r\n”;

$headers .= 'From: ’ .$_POST[‘email’]. “\r\n”;

$headers .= 'Reply-To: ’ .$_POST[‘email’]. “\r\n”;

$mail_to = "[email protected]";

$mail_subject = “G3 Services Website Comment”;

$mail_body .= "\n First Name: " .$_POST[‘first_name’]. “\n”;
$mail_body .= "\n
Last Name: " .$_POST[‘last_name’]. “\n”;
$mail_body .= "\n
Email Address: " .$_POST[‘email’]. “\n”;
$mail_body .= "\n
Phone Number " .$_POST[‘phone’]. “\n”;
$mail_body .= "\n
Comments:
" .$_POST[‘comment’]. “\n”;
[/php]

One thing I’m trying to is format the email headers so i know who the email is from. Right now, is just tells me their email address. Is there a way I can have it say From: First Last Name [email protected] ??

Thanks a bunch!
Juston

Sure, you can do this exactly as in your question:

[php]

<?php $headers .= 'From: '.trim($_POST['first_name'].' '.$_POST['last_name']).' <' .$_POST['email']. ">\n"; $headers .= 'Reply-To: '.trim($_POST['first_name'].' '.$_POST['last_name']).' <' .$_POST['email']. ">\n"; ?>

[/php]

For security reason, I would suggest to also replace any “\n” in the first_name and last_name and also in email (if you’re not validating this on form submission).

I tried that, and sent a test message, but when I hit reply, the “To” field is blank, and when i receive the message, the “From” field is blank too :frowning:

I’ll post my entire script:

[php]<?php session_start(); ?>

<? if ($_SERVER["REQUEST_METHOD"] <> "POST") die("You must enter through the html form."); // Captcha Variable // if ($_POST["captcha"] != $_SESSION["pass"]) $captcha = "no"; if ($_POST["captcha"] == $_SESSION["pass"]) $captcha = "yes"; // Header Information // $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: '.trim($_POST['first_name'].' '.$_POST['last_name']).' <' .$_POST['email']. ">\n"; $headers .= 'Reply-To: '.trim($_POST['first_name'].' '.$_POST['last_name']).' <' .$_POST['email']. ">\n"; $mail_to = "[email protected]"; $mail_subject = "G3 Services Website Comment"; // Mail Content // $mail_body .= "\n First Name: " .$_POST['first_name']. "\n"; $mail_body .= "\n
Last Name: " .$_POST['last_name']. "\n"; $mail_body .= "\n
Email Address: " .$_POST['email']. "\n"; $mail_body .= "\n
Phone Number " .$_POST['phone']. "\n"; $mail_body .= "\n
Comments:
" .$_POST['comment']. "\n"; // Action for Correct/Incorrect Captcha // if($captcha == 'no') { echo ''; } if ($captcha == "yes") mail($mail_to, $mail_subject, $mail_body, $headers); if ($captcha == "yes") echo("\n\n\n\n\n"); ?>

[/php]

Try to eliminate \r from email headers. Otherwise the code looks fine, and should interpret fields ‘From’ and ‘Reply-to’ correctly.

Thank you very much! That worked great

Here is what PHP manual says about using additional headers in the mail() function:

This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n).

Note: If messages are not received, try using a LF (\n) only. Some poor quality Unix mail transfer agents replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822.

Source: http://php.net/manual/en/function.mail.php

Sponsor our Newsletter | Privacy Policy | Terms of Service