JRS Posted April 15, 2006 Share Posted April 15, 2006 Hello,I've been trying to figure this out for few hours. Not sure why it's not working. I'm sure it something very simple I'm missing. If someone could shed some light on it, it would much appreciated.For some reason - my header is not working correctly. All the information in the header shows up in the message content. I'm guessing I'm doing something wrong with header - but can't figure out what it it is:Any help or suggestion would be much appreciated:Thanks in advanceJRS[code]$to_email = "[email protected]"; // test address$to_hotmail = "[email protected]"; // test address$subject = "Test Message 4-".time();$from_name = "John Smith";$from_mail = "[email protected]";$mime_boundary = "==MP_BOUND_xy123"; //Random string$cc_mail = "[email protected]";$cc_mail_name = "Charlie Carbon";$email_msg_html = "<html><h1>Test Message testing Html </h1></html>";$email_msg_txt = "This is the message contents in Text format only";$email_notice = "This is a multipart MIME email message";$headers = "From: $from_name <$from_mail>\r\n";$headers .= "\nMIME-Version: 1.0\r\n";$headers .= " Content-Type: multipart/alternative; boundary=\"{$mime_boundary}\"\n";$headers .= " CC:".$cc_mail_name."<$cc_mail>\n";$message = "\n{$email_notice} \n";$message .= "--{$mime_boundary}\n";$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";$message .= "Content-Transfer-Encoding: 7bit\n";$message .= "\n{$email_msg_txt}";$message .= "--{$mime_boundary}\n";$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";$message .= "Content-Transfer-Encoding: 7bit\n";$message .= "\n{$email_msg_html}";$message .= "--{$mime_boundary}--\n";echo "Message to AT&T \r\n";if (mail($to_email, $subject, $message, $headers)) echo "AT&T mail sent - ok \r\n";else echo "AT&T Error \r\n";echo "Message to hotmail \r\n";if (mail($to_hotmail, $subject, $message, $headers)) echo "Hotmail sent - ok \r\n";else echo "Hotmail Error \r\n";[/code]Main problem is header content is being displayed in the mail message. Along with all the parts of the mail such as HTML & plain Text. Quote Link to comment https://forums.phpfreaks.com/topic/7506-cant-figure-out-mail-header-problem/ Share on other sites More sharing options...
JRS Posted April 16, 2006 Author Share Posted April 16, 2006 I tried some code I found on the net - in terms of the header. But still the same problem for some reason th e message header is getting printed by Outlook Express: Here is the updated code. If anyone can spot what I'm doing wrong - it would be much appreciated. Thanks in advanceJRS[code]$headers=""; $headers .= "X-Sender: $to_email <$to_email>\n"; // $headers .="From: $from_name <$from_name>\n"; $headers .= "Reply-To: $from_name <$from_mail>\n"; $headers .= "Date: ".date("r")."\n"; $headers .= "Message-ID: <".date("YmdHis")."tester@".$_SERVER['SERVER_NAME'].">\n"; $headers .= "Subject: $subject\n"; // subject write here $headers .= "Return-Path: $from_name <$from_mail>\n"; $headers .= "Delivered-to: $from_name <$from_name>\n"; $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-type: multipart/alternative;charset=ISO-8859-9\n"; $headers .= "X-Priority: 1\n"; $headers .= "Importance: High\n"; $headers .= "X-MSMail-Priority: High\n"; $headers .= "X-Mailer: Test Mailer\n";$message = "";// Setup for text OR html$message .= "Content-Type: text/plain\n";$message .= "\n{$email_notice} \n";$message .= "--".$mime_boundary."\n";$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";$message .= "Content-Transfer-Encoding: 8bit\n\n\n";$message .= "$email_msg_txt";$message .= "--{$mime_boundary}\n\n";// HTML Version$message .= "--".$mime_boundary."\n";$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";$message .= "Content-Transfer-Encoding: 7bit\n\n\n";$message .= "$email_msg_html";$message .= "--$mime_boundary--\n";echo "Message to AT&T \r\n";if (mail($to_email, $subject, $message, $headers)) echo "AT&T mail sent - ok \r\n";else echo "AT&T Error \r\n";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/7506-cant-figure-out-mail-header-problem/#findComment-27468 Share on other sites More sharing options...
JRS Posted April 17, 2006 Author Share Posted April 17, 2006 Bump! Quote Link to comment https://forums.phpfreaks.com/topic/7506-cant-figure-out-mail-header-problem/#findComment-27597 Share on other sites More sharing options...
ypirc Posted April 17, 2006 Share Posted April 17, 2006 After just quickly glancing over it I notice that you are using "\n" alone in some portions of the header. SMTP RFC compliant mail servers _require_ that all SMTP commands be terminated with a CRLF "\r\n" Quote Link to comment https://forums.phpfreaks.com/topic/7506-cant-figure-out-mail-header-problem/#findComment-27602 Share on other sites More sharing options...
JRS Posted April 17, 2006 Author Share Posted April 17, 2006 [!--quoteo(post=365439:date=Apr 16 2006, 11:16 PM:name=ypirc)--][div class=\'quotetop\']QUOTE(ypirc @ Apr 16 2006, 11:16 PM) [snapback]365439[/snapback][/div][div class=\'quotemain\'][!--quotec--]After just quickly glancing over it I notice that you are using "\n" alone in some portions of the header. SMTP RFC compliant mail servers _require_ that all SMTP commands be terminated with a CRLF "\r\n"[/quote]Hi Ypirc,Thanks for the response. I've tried it with and without \r. I read bug report on PHP.NET that stated \r was causing problem on unix systems. My host has unix - so I decided to drop \r. The problem appears to be withmixed content - when multiple contents such as plain text and HTML are involved.I've spent quite a few hours on this trying all kinds of combinations and sample codes from quite a few sites - no luck.If anyone has any ideas - I'll give it a shotThanks in advanceJRS Quote Link to comment https://forums.phpfreaks.com/topic/7506-cant-figure-out-mail-header-problem/#findComment-27822 Share on other sites More sharing options...
ypirc Posted April 18, 2006 Share Posted April 18, 2006 Can you debug your mail server and who us -exactly- what headers this client is trying to send. It seems as-if you are trying to send too many extra headers that are already included in the mail() function. This might work better if you were opening a socket in PHP to your localhost to send the mail and not using the mail() function. Also, I use *nix also, and that's why I still said you need to add "\r\n" (CRLF) to the headers, yes I read on PHP.net about _some_ *nix based MTA's not accepting it, but if you read, they say they are poor quality ones and do not conform to the RFC. If that's the case, you shouldn't be running the MTA to begin with. Anyway, it'd help best if you can debug your mail server and show us the exact headers it's trying to send. Quote Link to comment https://forums.phpfreaks.com/topic/7506-cant-figure-out-mail-header-problem/#findComment-28044 Share on other sites More sharing options...
JRS Posted April 18, 2006 Author Share Posted April 18, 2006 [!--quoteo(post=365884:date=Apr 18 2006, 01:02 AM:name=ypirc)--][div class=\'quotetop\']QUOTE(ypirc @ Apr 18 2006, 01:02 AM) [snapback]365884[/snapback][/div][div class=\'quotemain\'][!--quotec--]Can you debug your mail server and who us -exactly- what headers this client is trying to send. It seems as-if you are trying to send too many extra headers that are already included in the mail() function. This might work better if you were opening a socket in PHP to your localhost to send the mail and not using the mail() function. Also, I use *nix also, and that's why I still said you need to add "\r\n" (CRLF) to the headers, yes I read on PHP.net about _some_ *nix based MTA's not accepting it, but if you read, they say they are poor quality ones and do not conform to the RFC. If that's the case, you shouldn't be running the MTA to begin with. Anyway, it'd help best if you can debug your mail server and show us the exact headers it's trying to send.[/quote]Hello Ypirc,Thanks for the help. I'm not sure how to get the debug information - as I understand it - the mail function only returns true or false. Is there another place I should look for the debug information?WRT: opening the socket and sending directly - I'm not familiar with this method - but I'll google and see what I can find.Basically, all I want to do is send an email with html and text (as I understand it - to handle clients that do not support html - one should send plain text version of the message). So I'm just using header definitions that were posted on the net by others. I assumed these were the minimum settings required.Thanks again for your help. I'll try and provide whatever info. I can.JRS Quote Link to comment https://forums.phpfreaks.com/topic/7506-cant-figure-out-mail-header-problem/#findComment-28188 Share on other sites More sharing options...
JRS Posted April 19, 2006 Author Share Posted April 19, 2006 BUMP! Quote Link to comment https://forums.phpfreaks.com/topic/7506-cant-figure-out-mail-header-problem/#findComment-28476 Share on other sites More sharing options...
alpine Posted April 19, 2006 Share Posted April 19, 2006 i cant see where you are making your $mime_boundary - if this is missing it would failexample[code]$mime_boundary=md5(time());[/code] Quote Link to comment https://forums.phpfreaks.com/topic/7506-cant-figure-out-mail-header-problem/#findComment-28487 Share on other sites More sharing options...
JRS Posted April 20, 2006 Author Share Posted April 20, 2006 [!--quoteo(post=366331:date=Apr 19 2006, 02:56 AM:name=alpine)--][div class=\'quotetop\']QUOTE(alpine @ Apr 19 2006, 02:56 AM) [snapback]366331[/snapback][/div][div class=\'quotemain\'][!--quotec--]i cant see where you are making your $mime_boundary - if this is missing it would failexample[code]$mime_boundary=md5(time());[/code][/quote]Hi Alpine,I do have the mime_boundary defined. It was in the first message. I tested it again after you mentioned. Here is the resulting email - if that might shed any lights for anyone. Even ideas on how to debug would be much appreciated.ThanksJRSEmail I received:From: John Smith <John [email protected]>Reply-To: John Smith <[email protected]>Date: Wed, 19 Apr 2006 23:47:22 -0400Message-ID: <[email protected]>Subject: Test Message 4-1145504842Delivered-to: John Smith <John Smith>MIME-Version: 1.0Content-type: multipart/alternative;charset=ISO-8859-9X-Priority: 1Importance: HighX-MSMail-Priority: HighX-Mailer: Test MailerContent-Type: text/plainThis is a multipart MIME email message - The email program is not reading it correclty --a2b7867f3843bb3c9e49d2612d82e247Content-Type: text/plain; charset="iso-8859-1"Content-Transfer-Encoding: 8bitThis is the message contents in Text format only--a2b7867f3843bb3c9e49d2612d82e247--a2b7867f3843bb3c9e49d2612d82e247Content-Type: text/html; charset="iso-8859-1"Content-Transfer-Encoding: 7bit<html><h1>Test Message testing Html </h1></html>--a2b7867f3843bb3c9e49d2612d82e247-- Quote Link to comment https://forums.phpfreaks.com/topic/7506-cant-figure-out-mail-header-problem/#findComment-28860 Share on other sites More sharing options...
alpine Posted April 20, 2006 Share Posted April 20, 2006 Hi,you must include "Content-Transfer-Encoding: 8bit" in the header aswell as in the message, and i suggest sticking to one, either 7 or 8 bitHere is a working example (i modified it a bit to adapt, hope it still works..)Note that you should generate a unique string to use in message id, therefore the function random_string.Test it and see what it gives:[code]function random_string($length){$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";for($i=0;$i<$length;$i++){$string .= $chars{rand(0,35)};}return $string;}function send_email($to_email,$subject,$body){// or set these as Global and include from config file....$name = "Your name";$email = "[email protected]";$eol="\r\n";$headers = "From: $name <$email>".$eol;$headers .= "Reply-To: $name <$email>".$eol;$headers .= "Return-Path: $name <$email>".$eol;$headers .= "X-Mailer: PHP v".phpversion().$eol;$headers .= "Date: ".date("r").$eol;$headers .= "Message-ID: <".date("YmdHis").random_string(12)."@".$_SERVER['SERVER_NAME'].">".$eol;$mime_boundary=md5(time());$headers .= 'MIME-Version: 1.0'.$eol;$headers .= "Content-Transfer-Encoding: 8bit".$eol;$headers .= "Content-Type: multipart/alternative; boundary=\"".$mime_boundary."\"".$eol;$msg = "";# Text Versjon$msg .= "--".$mime_boundary.$eol;$msg .= "Content-Transfer-Encoding: 8bit".$eol;$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;$msg .= "This is a multi-part message in MIME format.".$eol.$eol;# HTML Versjon$msg .= "--".$mime_boundary.$eol;$msg .= "Content-Transfer-Encoding: 8bit".$eol;$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;$msg .= "This is a multi-part message in MIME format.".$eol.$eol;$msg .= $eol.$eol.$body.$eol.$eol;# Ready$msg .= "--".$mime_boundary."--".$eol.$eol;$msg = wordwrap($msg, 70);if(ini_get('safe_mode')){mail($to_email, $subject, $msg, $headers);}else{mail($to_email, $subject, $msg, $headers, "-f" . $email);}return true;}// call the function and send email$do_send = send_email("[email protected]","This is subject","This is message body");[/code] Quote Link to comment https://forums.phpfreaks.com/topic/7506-cant-figure-out-mail-header-problem/#findComment-28999 Share on other sites More sharing options...
kenrbnsn Posted April 20, 2006 Share Posted April 20, 2006 The headers in an email message are seperated from the message by a blank line. In order to get that you need to end your headers with two newline characters either "\n\n" or "\r\n\r\n". You have one newline character at the end of your headers.Ken Quote Link to comment https://forums.phpfreaks.com/topic/7506-cant-figure-out-mail-header-problem/#findComment-29038 Share on other sites More sharing options...
JRS Posted April 20, 2006 Author Share Posted April 20, 2006 Alpine,I tested your code without any modifications other than my email address, I'm seeing the same problem as before. I also tried Kenbersn's suggestion of adding extra eol after headers- still the same results. Below is what I'm getting in Microsoft Outlook Express. I tried emailing to Hotmail - but the never went through!Thanks for all your help (and you too Ken & Ypirc)....I think I'm going to download the PHP Mailer and try using that. There is tutorial on this site.Using the mail function seems to be insanely difficult!ThanksJRSHere is the mail I received when trying Alpine's example code:Date: Thu, 20 Apr 2006 14:02:53 -0400Message-ID: <[email protected]>MIME-Version: 1.0Content-Transfer-Encoding: 8bitContent-Type: multipart/alternative; boundary="ce90473f0599e25c330df00f9ea7dd21"--ce90473f0599e25c330df00f9ea7dd21Content-Transfer-Encoding: 8bitContent-Type: text/plain; charset=iso-8859-1This is a multi-part message in MIME format.--ce90473f0599e25c330df00f9ea7dd21Content-Transfer-Encoding: 8bitContent-Type: text/html; charset=iso-8859-1This is a multi-part message in MIME format.This is the message contents in Text format only--ce90473f0599e25c330df00f9ea7dd21-- Quote Link to comment https://forums.phpfreaks.com/topic/7506-cant-figure-out-mail-header-problem/#findComment-29080 Share on other sites More sharing options...
alpine Posted April 20, 2006 Share Posted April 20, 2006 How is your result if you dont add headers at all ? just send a pure raw php mail(); ??I experienced similar problems once after my webhost switched on mbstring.func_overload=7Either i got blanc emails, or i got everything included headers printed out in the message field - it only worked "as normal" with a minimum of headers. Quote Link to comment https://forums.phpfreaks.com/topic/7506-cant-figure-out-mail-header-problem/#findComment-29113 Share on other sites More sharing options...
JRS Posted April 21, 2006 Author Share Posted April 21, 2006 [!--quoteo(post=366966:date=Apr 20 2006, 03:18 PM:name=alpine)--][div class=\'quotetop\']QUOTE(alpine @ Apr 20 2006, 03:18 PM) [snapback]366966[/snapback][/div][div class=\'quotemain\'][!--quotec--]How is your result if you dont add headers at all ? just send a pure raw php mail(); ??I experienced similar problems once after my webhost switched on mbstring.func_overload=7Either i got blanc emails, or i got everything included headers printed out in the message field - it only worked "as normal" with a minimum of headers.[/quote]Hi Alpine,It is fine without the headers or minimum header manipulation - from field. I am able to send plain text message or html but not both.Yes, I get all the header information printed out in the message. Is there a way I can check the mbstring.func_overload setting? Is it part of the phpinfo() result?ThanksJRS Quote Link to comment https://forums.phpfreaks.com/topic/7506-cant-figure-out-mail-header-problem/#findComment-29262 Share on other sites More sharing options...
JRS Posted April 24, 2006 Author Share Posted April 24, 2006 Hello All,Just wanted to let others who might have this problem know how it was solved!Used PHPMailer. Obviously something was not being set correctly in theheader. Alpine had a version that was working - it wasn't working on mine.So my advice, don't waste time trying to figure headers out (unless you really want to learn email headers) just use PHPMailer class - there is also a tutorialon this site.ThanksJRS Quote Link to comment https://forums.phpfreaks.com/topic/7506-cant-figure-out-mail-header-problem/#findComment-30124 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.