Page 1 of 1

Problem getting headers to work in an email

Posted: Wed Jul 09, 2003 10:59 am
by Swede78
I have the following code which mostly works, but the headers do not work. When I get an email from someone who fills out my online contact form, I want the From to be "Online Form <webmaster@address.com>". But, I want the Reply-To name and address to be that of the person who emailed me. Any help would be appreciated.

Code: Select all

<?php
$FullName = $_POST['FullName'];
$Email = $_POST['Email'];
$Message = 'bla bla';

$To = "Me <myself@address.com>";
$From = "Online Form <WebMaster@address.com>";
$Subject = "Online Contact Form";
$Header = "From: $FullName\nReply-To: $Email\nReturn-path <myself@address.com>";
$My_email = new COM("CDONTS.NewMail") or die("Could not create an email"); 
$My_email->from = $From; 
$My_email->to = $To; 
$My_email->subject = $Subject; 
$My_email->body = $Message;
$My_email->send();
?>

Posted: Wed Jul 09, 2003 11:20 am
by redhair
add:

Code: Select all

<?php
$header .= "Reply-To:$email\n";
?>
below: $Header = "From: $FullName\nReply-To: $Email\nReturn-path <myself@address.com>";

might work..

I usually use $mailheaders instead of $header.
Just $header works to?

Posted: Wed Jul 09, 2003 12:02 pm
by m3rajk
$header is a bad habit to get into, there's a function header(), but php knows the diff between functions and variables, so it'll work.

i suggest making it like this (assumes linux host. change \n to \r\n for windows)

Code: Select all

<?php
$sender=$_POST['sender']; $recipient=$_POST['recipient'];
$subject=$_POST['subject]; $message=$_POST['message'];

$replyto="Reply-To: $sender";
$from="From: $sender";
# set other header things here

$mailheader="$from\n$replyto"; #if youhave more then tow, seperate with \n

mail($sender, $subject, $message, $mailheader);
?>
this allows you to isolate each individual piece that can cause an error so that you can find/fix errors easier.

this uses sendmail