Jump to content

[SOLVED] PHP Mail Headers formatting question


fatmikey

Recommended Posts

I am fairly new at PHP so I hope you can help.  I have a script in which I'm trying to format the headers to form an email message.  Basically what I want to do is have the FROM header look something like this:  From: Joe Blow <[email protected]> but for some reason my formatting isn't working.  Here is what I have so far:

 

$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email'])); 
$From = $Name;
$From .= " <";
$From .= $Email;
$From .= ">";

 

Can you tell me what I'm doing wrong here?

 

Thanks,

FatMikey

I just did another test, I commented out the last > as follows:

 

$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email'])); 
$From = $Name;
$From .= " <";
$From .= $Email;
//$From .= ">";

 

And now the result is:

 

Joe Blow <[email protected]

 

But, when I put back the last > it removed the whole email address.  Any suggestions?

 

Thanks,

FatMikey

When you display any string that starts with a "<" and ends with a ">" on the screen, your browser will eat it because it thinks it's a tag. To get around this, use htmlentities() when displaying it on the screen.

<?php
$from = 'From: Joe Blow <[email protected]>';
echo htmlentities($from,ENT_QUOTES);
?>

 

Also, your $from header requires the string "From: " in it.

 

Ken

Almost there, thanks for the suggestion, this does display on the screen a correct looking string as follows:

 

From: Joe Smith <[email protected]> Bcc: [email protected] Content-Type: text/plain; charset=iso-8859-1

 

But, the problem is when I try to add it to the headers of my email string I get an error condition, here is my code:

 

$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email'])); 
$From = "From: " . $Name . " <" . $Email . ">";
$EmailTo = "[email protected]";
$Headers = htmlentities($From,ENT_QUOTES);
$Headers .= "\n";
$Headers .= "Bcc: [email protected]\n";
$Headers .= "Content-Type: text/plain; charset=iso-8859-1\n";
$Message = Trim(stripslashes($_POST['Message']));
$Subject = "Inquiry from Website";

echo $Headers;

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

if (mail($EmailTo, $Subject, $Body, $Headers)) {
//  print "<meta http-equiv=\"refresh\" content=\"0;URL=thankyouemail.htm\">";
} else {
//  print "<meta http-equiv=\"refresh\" content=\"0;URL=smerror.htm\">";
echo "error";
}
?>

 

 

You should only used htmlentities() when displaying the data. It is not needed when creating the header:

<?php
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email'])); 
$From = "From: " . $Name . " <" . $Email . ">";
$EmailTo = "[email protected]";
$Headers = $From;
$Headers .= "\n";
$Headers .= "Bcc: [email protected]\n";
$Headers .= "Content-Type: text/plain; charset=iso-8859-1\n";
$Message = Trim(stripslashes($_POST['Message']));
$Subject = "Inquiry from Website";

echo '<pre>' . htmlentities($Headers,ENT_QUOTES) . '</pre>';

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

if (mail($EmailTo, $Subject, $Body, $Headers)) {
//  print "<meta http-equiv=\"refresh\" content=\"0;URL=thankyouemail.htm\">";
} else {
//  print "<meta http-equiv=\"refresh\" content=\"0;URL=smerror.htm\">";
echo "error";
}
?>

 

Ken

 

 

I am only displaying the header on the screen by using an echo just for debug purposes, once this is actually working I won't need to see this info displayed on the screen.

 

What I really need is to get this put into a proper email header so the message can be sent with no errors.  I have most of this script working just not the part to encapsulate the email address into brackets as follows:  <[email protected]>  Any ideas on how to do this?

 

Thanks,

FatMikey

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.