Jump to content

Archived

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

JRS

Can't figure out mail header problem

Recommended Posts

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 advance
JRS

[code]
$to_email = "test@testsite.com";    // test address
$to_hotmail = "js@hotmail.com";     // test address

$subject = "Test Message 4-".time();
$from_name = "John Smith";
$from_mail = "jstest1@jsmith.com";
$mime_boundary = "==MP_BOUND_xy123";        //Random string
$cc_mail = "jstest2@jsmith.com";
$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.

Share this post


Link to post
Share on other sites
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 advance
JRS
[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]

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites
[!--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 with
mixed 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 shot
Thanks in advance
JRS

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[!--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

Share this post


Link to post
Share on other sites
i cant see where you are making your $mime_boundary - if this is missing it would fail

example
[code]
$mime_boundary=md5(time());
[/code]

Share this post


Link to post
Share on other sites
[!--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 fail

example
[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.

Thanks
JRS
Email I received:

From: John Smith <John Smith@test.testweb.net>
Reply-To: John Smith <jstest1@jsmith.com>
Date: Wed, 19 Apr 2006 23:47:22 -0400
Message-ID: <20060419234722@www.test.com>
Subject: Test Message 4-1145504842
Delivered-to: John Smith <John Smith>
MIME-Version: 1.0
Content-type: multipart/alternative;charset=ISO-8859-9
X-Priority: 1
Importance: High
X-MSMail-Priority: High
X-Mailer: Test Mailer


Content-Type: text/plain

This is a multipart MIME email message - The email program is not reading it correclty
--a2b7867f3843bb3c9e49d2612d82e247
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit


This is the message contents in Text format only--a2b7867f3843bb3c9e49d2612d82e247

--a2b7867f3843bb3c9e49d2612d82e247
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit


<html><h1>Test Message testing Html </h1></html>--a2b7867f3843bb3c9e49d2612d82e247--

Share this post


Link to post
Share on other sites
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 bit

Here 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 = "Your@email.what";

$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("to@some.what","This is subject","This is message body");
[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!
Thanks
JRS
Here is the mail I received when trying Alpine's example code:

Date: Thu, 20 Apr 2006 14:02:53 -0400

Message-ID: <2006042014025358QYZ0N6M0QO@www.test.com>

MIME-Version: 1.0

Content-Transfer-Encoding: 8bit

Content-Type: multipart/alternative; boundary="ce90473f0599e25c330df00f9ea7dd21"






--ce90473f0599e25c330df00f9ea7dd21

Content-Transfer-Encoding: 8bit

Content-Type: text/plain; charset=iso-8859-1

This is a multi-part message in MIME format.



--ce90473f0599e25c330df00f9ea7dd21

Content-Transfer-Encoding: 8bit

Content-Type: text/html; charset=iso-8859-1

This is a multi-part message in MIME format.







This is the message contents in Text format only



--ce90473f0599e25c330df00f9ea7dd21--


Share this post


Link to post
Share on other sites
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=7

Either 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.

Share this post


Link to post
Share on other sites
[!--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=7

Either 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?

Thanks
JRS

Share this post


Link to post
Share on other sites
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 the
header. 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 tutorial
on this site.

Thanks
JRS

Share this post


Link to post
Share on other sites

×

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.