By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,017 Members | 1,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,017 IT Pros & Developers. It's quick & easy.

Help with mail() - message not displaying

P: n/a
I'm trying to work out a mail system which can send an attachment as well
as an HTML formatted message (and a default plain text version).

I found some pretty good code on PHP.net and modified it a little but I
can't seem to get it to work.

It attaches the file properly, but only displays the Plain Text message,
even in an HTML-capable mail client.

I'm guessing it has something to do with the placement of the boundaries,
but I can't figure it out.

TIA
<?php

// Is the OS Windows or Mac or Linux
if (strtoupper(substr(PHP_OS,0,3) == 'WIN')) {
$eol = "\r\n";
} elseif (strtoupper(substr(PHP_OS,0,3) == 'MAC')) {
$eol = "\r";
} else {
$eol = "\n";
}
// Set Up Message Details
$recipient = "fo*@bar.com";
$subject = "E-mail Message ".date("Y/m/d H:i:s");
$from_name = "Mr. Foo Bar";
$from_email = "fo*@bar.com";
$text_msg = $eol."TEXT MESSAGE BODY".$eol; // plain text version of the
message
$html_msg = $eol."<html><body><strong>HTML MESSAGE BODY</strong></body>
</html>".$eol; // teh HTML goodness

// File for Attachment
$file_name = "file.gif"; // name of the file being attached
$f_name = $path.$file_name; // path + name of the file being attached
$handle = fopen($f_name, 'rb');
$f_contents = fread($handle, filesize($f_name));
$f_contents = chunk_split(base64_encode($f_contents));
$f_type = filetype($f_name);
fclose($handle);
// Headers
$headers = "";
$headers .= "From: $from_name <$from_email>".$eol;
$headers .= "Reply-To: $from_name <$from_email>".$eol;
$headers .= "Return-Path: $from_name <$from_email>".$eol;
$headers .= "X-Sender: <$from_email>".$eol;
$headers .= "Errors-To: <$from_email>".$eol;
$headers .= "Message-ID: <".md5(uniqid(rand())).".".preg_replace("/[^a-z0-
9]/i", "", $from_name).">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol;
$headers .= "X-Priority: 3\n";

// Boundary for marking the split & Multitype Headers
$mime_boundary = "php-".md5(rand());
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".
$mime_boundary."\"".$eol;
$msg = "";

// Attachment
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: $f_type; name=\"".$file_name."\"".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Disposition: attachment; filename=\"".$file_name."\"".
$eol.$eol;
$msg .= $f_contents.$eol.$eol;
$msg .= "--".$mime_boundary.$eol;
// Text Version
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= "This is a multi-part message in MIME format.".$eol;
$msg .= "If you are reading this, please update your email-reading-
software.".$eol;
$msg .= $text_msg;
$msg .= "--".$mime_boundary.$eol;

// HTML Version
$htmlalt_mime_boundary = $mime_boundary."_htmlalt";
$msg .= "Content-Type: multipart/alternative; boundary=\"".
$htmlalt_mime_boundary."\"".$eol;
$msg .= "--".$htmlalt_mime_boundary.$eol.$eol;
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= $html_msg;
$msg .= $eol.$eol."--".$htmlalt_mime_boundary."--".$eol.$eol;

// Finish Message
$msg .= "--".$mime_boundary."--".$eol.$eol;

// SEND THE EMAIL
ini_set(sendmail_from, $from_email); // the INI lines are to force the
From Address to be used !
mail($recipient, $subject, $msg, $headers);
ini_restore(sendmail_from);

// DEBUG STUFF
//print("<pre>$msg</pre>");
?>
--
Karl Groves
www.karlcore.com
Nov 29 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Tom
uncomment the last line:

//print("<pre>$msg</pre>");

Also, unless you have other reasons for writing your own code here, you
might check out something phpmailer. It may help save you from
reinventing the wheel.

Tom
Karl Groves wrote:
I'm trying to work out a mail system which can send an attachment as well
as an HTML formatted message (and a default plain text version).

I found some pretty good code on PHP.net and modified it a little but I
can't seem to get it to work.

It attaches the file properly, but only displays the Plain Text message,
even in an HTML-capable mail client.

I'm guessing it has something to do with the placement of the boundaries,
but I can't figure it out.

TIA
<?php

// Is the OS Windows or Mac or Linux
if (strtoupper(substr(PHP_OS,0,3) == 'WIN')) {
$eol = "\r\n";
} elseif (strtoupper(substr(PHP_OS,0,3) == 'MAC')) {
$eol = "\r";
} else {
$eol = "\n";
}
// Set Up Message Details
$recipient = "fo*@bar.com";
$subject = "E-mail Message ".date("Y/m/d H:i:s");
$from_name = "Mr. Foo Bar";
$from_email = "fo*@bar.com";
$text_msg = $eol."TEXT MESSAGE BODY".$eol; // plain text version of the
message
$html_msg = $eol."<html><body><strong>HTML MESSAGE BODY</strong></body>
</html>".$eol; // teh HTML goodness

// File for Attachment
$file_name = "file.gif"; // name of the file being attached
$f_name = $path.$file_name; // path + name of the file being attached
$handle = fopen($f_name, 'rb');
$f_contents = fread($handle, filesize($f_name));
$f_contents = chunk_split(base64_encode($f_contents));
$f_type = filetype($f_name);
fclose($handle);
// Headers
$headers = "";
$headers .= "From: $from_name <$from_email>".$eol;
$headers .= "Reply-To: $from_name <$from_email>".$eol;
$headers .= "Return-Path: $from_name <$from_email>".$eol;
$headers .= "X-Sender: <$from_email>".$eol;
$headers .= "Errors-To: <$from_email>".$eol;
$headers .= "Message-ID: <".md5(uniqid(rand())).".".preg_replace("/[^a-z0-
9]/i", "", $from_name).">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol;
$headers .= "X-Priority: 3\n";

// Boundary for marking the split & Multitype Headers
$mime_boundary = "php-".md5(rand());
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".
$mime_boundary."\"".$eol;
$msg = "";

// Attachment
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: $f_type; name=\"".$file_name."\"".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Disposition: attachment; filename=\"".$file_name."\"".
$eol.$eol;
$msg .= $f_contents.$eol.$eol;
$msg .= "--".$mime_boundary.$eol;
// Text Version
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= "This is a multi-part message in MIME format.".$eol;
$msg .= "If you are reading this, please update your email-reading-
software.".$eol;
$msg .= $text_msg;
$msg .= "--".$mime_boundary.$eol;

// HTML Version
$htmlalt_mime_boundary = $mime_boundary."_htmlalt";
$msg .= "Content-Type: multipart/alternative; boundary=\"".
$htmlalt_mime_boundary."\"".$eol;
$msg .= "--".$htmlalt_mime_boundary.$eol.$eol;
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= $html_msg;
$msg .= $eol.$eol."--".$htmlalt_mime_boundary."--".$eol.$eol;

// Finish Message
$msg .= "--".$mime_boundary."--".$eol.$eol;

// SEND THE EMAIL
ini_set(sendmail_from, $from_email); // the INI lines are to force the
From Address to be used !
mail($recipient, $subject, $msg, $headers);
ini_restore(sendmail_from);

// DEBUG STUFF
//print("<pre>$msg</pre>");
?>
--
Karl Groves
www.karlcore.com
Nov 29 '06 #2

P: n/a
"Tom" <kl******@gmail.comwrote in
news:11**********************@80g2000cwy.googlegro ups.com:
uncomment the last line:

//print("<pre>$msg</pre>");
That line is just a debug line I put there to see the message printed. It
is done *after* the mail is sent, so that doesn't solve my problem
Also, unless you have other reasons for writing your own code here,
you might check out something phpmailer. It may help save you from
reinventing the wheel.
This is actually going into another pre-existing script, so the wheel was
already reinvented.


--
Karl Groves
www.karlcore.com
Nov 29 '06 #3

P: n/a
Tom
My apologies for the first response. Felt so bad that I cracked open
phpmailer to glance at its code to see if any obvious differences stood
out. I don't have any experience with this particular problem and
prefer my email in good old plain text, so that's about the best I can
do.

I did notice that phpmailer does encode some parts of the message. I
refer specifically to this line of code:

@mail($to, $this->EncodeHeader($this->Subject), $body, $header,
$params);

Could it be an encoding problem, specifically something somewhere not
being encoded in the right way?

If that doesn't help, my further apologies. I'll let the experts take
over from here.

Tom

On Nov 29, 12:16 pm, Karl Groves <k...@NOSPAMkarlcore.comwrote:
"Tom" <klenw...@gmail.comwrote innews:11**********************@80g2000cwy.googleg roups.com:
uncomment the last line:
//print("<pre>$msg</pre>");That line is just a debug line I put there to see the message printed. It
is done *after* the mail is sent, so that doesn't solve my problem
Also, unless you have other reasons for writing your own code here,
you might check out something phpmailer. It may help save you from
reinventing the wheel.This is actually going into another pre-existing script, so the wheel was
already reinvented.

--
Karl Groveswww.karlcore.com
Nov 30 '06 #4

P: n/a
Tom wrote:
uncomment the last line:

//print("<pre>$msg</pre>");

Also, unless you have other reasons for writing your own code here, you
might check out something phpmailer. It may help save you from
reinventing the wheel.

Tom
Please excuse the ignorance,
where would I find phpmailer ?

bill
Nov 30 '06 #5

P: n/a
Tom
http://phpmailer.sourceforge.net/

Tom

On Nov 30, 4:09 am, bill <nob...@spamcop.netwrote:
Tom wrote:
uncomment the last line:
//print("<pre>$msg</pre>");
Also, unless you have other reasons for writing your own code here, you
might check out something phpmailer. It may help save you from
reinventing the wheel.
TomPlease excuse the ignorance,
where would I find phpmailer ?

bill
Dec 1 '06 #6

P: n/a
Tom wrote:
http://phpmailer.sourceforge.net/
THank you kindly
bill

>
On Nov 30, 4:09 am, bill <nob...@spamcop.netwrote:
>Tom wrote:
>>uncomment the last line:
//print("<pre>$msg</pre>");
Also, unless you have other reasons for writing your own code here, you
might check out something phpmailer. It may help save you from
reinventing the wheel.
TomPlease excuse the ignorance,
where would I find phpmailer ?

bill
Dec 2 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.