fatmikey Posted October 13, 2008 Share Posted October 13, 2008 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 Link to comment https://forums.phpfreaks.com/topic/128224-solved-php-mail-headers-formatting-question/ Share on other sites More sharing options...
trq Posted October 13, 2008 Share Posted October 13, 2008 What does your $From value end up looking like? Link to comment https://forums.phpfreaks.com/topic/128224-solved-php-mail-headers-formatting-question/#findComment-664086 Share on other sites More sharing options...
fatmikey Posted October 13, 2008 Author Share Posted October 13, 2008 The result is this: Joe Blow ' Link to comment https://forums.phpfreaks.com/topic/128224-solved-php-mail-headers-formatting-question/#findComment-664087 Share on other sites More sharing options...
fatmikey Posted October 13, 2008 Author Share Posted October 13, 2008 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 Link to comment https://forums.phpfreaks.com/topic/128224-solved-php-mail-headers-formatting-question/#findComment-664092 Share on other sites More sharing options...
kenrbnsn Posted October 13, 2008 Share Posted October 13, 2008 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 Link to comment https://forums.phpfreaks.com/topic/128224-solved-php-mail-headers-formatting-question/#findComment-664197 Share on other sites More sharing options...
fatmikey Posted October 13, 2008 Author Share Posted October 13, 2008 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/128224-solved-php-mail-headers-formatting-question/#findComment-664221 Share on other sites More sharing options...
revraz Posted October 13, 2008 Share Posted October 13, 2008 Post the error Link to comment https://forums.phpfreaks.com/topic/128224-solved-php-mail-headers-formatting-question/#findComment-664226 Share on other sites More sharing options...
kenrbnsn Posted October 13, 2008 Share Posted October 13, 2008 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 Link to comment https://forums.phpfreaks.com/topic/128224-solved-php-mail-headers-formatting-question/#findComment-664243 Share on other sites More sharing options...
fatmikey Posted October 13, 2008 Author Share Posted October 13, 2008 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 Link to comment https://forums.phpfreaks.com/topic/128224-solved-php-mail-headers-formatting-question/#findComment-664256 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.