Problem getting headers to work in an email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Swede78
Forum Contributor
Posts: 198
Joined: Wed Mar 12, 2003 12:52 pm
Location: IL

Problem getting headers to work in an email

Post 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();
?>
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post 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?
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
Post Reply