Link to home
Start Free TrialLog in
Avatar of alicia1234
alicia1234Flag for United States of America

asked on

Why is output buffering not working?

I have copied an example for sending HTML email. It looks straight-forward enough, but the output isn't being buffered, nor is the email being sent (nor are there errors in the SMTP log). Everything from the first "--PHP-alt" through the final "--PHP-alt" is being displayed on the page.

What's wrong?

The code below is the complete page that I am executing.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>HTML email test</title>
</head>
 
<body>
<?php
// compose email
$to = "alicia@wallewu.com";
$subject = "Testing HTML email from localhost";
// unique boundary string (md5 generates random number)
$random_hash = md5(date('r',time()));
$headers = "cc: alicia@orangemooncreations.com";
// add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
// define body of message
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
 
Hello World!!! 
This is simple text email message. 
 
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
 
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
 
--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
</body>
</html>

Open in new window

Avatar of alicia1234
alicia1234
Flag of United States of America image

ASKER

"View source" for the page shows this, so obviously something is wrong.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>HTML email test</title>
</head>
 
<body>
--PHP-alt-4342323995b69d7090a2ac8e0be0a7f3 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
 
Hello World!!! 
This is simple text email message. 
 
--PHP-alt-4342323995b69d7090a2ac8e0be0a7f3 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
 
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
 
--PHP-alt-4342323995b69d7090a2ac8e0be0a7f3--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"<?" only works if short_open_tag is enabled in the php.ini file:

http://php.net/manual/en/ini.core.php#ini.short-open-tag
 <!--[if gte mso 9]>     1024x768 <![endif]--><!--[if gte mso 9]>   Normal  0      false  false  false                    <![endif]--><!--[if gte mso 9]>  <![endif]--><!--[if gte mso 10]><![endif]-->Thanks. I should have caught that! Now the email does getsent, but it's not showing as HTML (I do have HTML enabled in my email client).

There was a note on the posting where I got the code that said this:
 
"Your HTML mail won't work until you add MIME-Version: 1.0 inyour header."



But I don't know how to add that to the header. Where and how do I insert that?
Thanks again.

Oops - don't know what happened with that last post. I copied from a word doc - guess it brought too much stuff in with it. Sorry about that. Should have said:

Thanks. I should have caught that! Now the email does get sent, but it's not showing as HTML (I do have HTML enabled in my email client).

There was a note on the posting where I got the code that said this:
"Your HTML mail won't work until you add MIME-Version: 1.0 in your header."

But I don't know how to add that to the header. Where and how do I insert that?

Thanks again.
Short tags are a bad idea in general, since they will interfere with XML.  Best to keep good habits (spoken from experience).

You can use phpinfo() to find out about whether short tags are enabled in your PHP rig.  Look for this field:

short_open_tag

If it is set to "on" then something else is wrong.

Best regards, ~Ray
Here is a snip from my code - seems to work OK.  Just add to headers.

Best, ~Ray

// PREPARE HEADERS
$html_headers = "MIME-Version: 1.0\r\n";
$html_headers .= "Content-type: text/html; charset=\"iso-8859-1\"\r\n";
 
$html_headers .= "Content-Transfer-Encoding: 7bit\r\n";
 
$html_headers .= "X-Priority: 3\n";
$html_headers .= "X-MSMail-Priority: Normal\n";
 
$html_headers .= "From: $broadcast_from_name <$broadcast_from_email>\r\n";
$html_headers .= "Reply-To: $broadcast_from_email\r\n";
$html_headers .= "Errors-To: $broadcast_from_email\r\n";
$html_headers .= "Return-Path: <$broadcast_from_email>\r\n";

Open in new window

>> But I don't know how to add that to the header. Where and how do I insert that?

Try like this:

$headers = "cc: alicia@orangemooncreations.com";
$headers .= "\r\nMIME-Version: 1.0"
// add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
Thanks Ray. Still not working right. The email itself shows the following.

I'm not clear on the "boundary" thing. Is it being specified correctly in the headers?

<?php
// compose email
$to = "me@email.com";
$subject = "Testing HTML email from localhost";
// unique boundary string (md5 generates random number)
$random_hash = md5(date('r',time()));
$headers = "cc: you@email.com\r\n";
// add boundary string and mime type specification
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
$headers .= "\r\n";
// define body of message
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
 
Hello World!!! 
This is simple text email message. 
 
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
 
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
 
--PHP-alt-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

Open in new window

Here's the expanded email header:

-Persona: <Wallewu>
Return-Path: <alicia@localhost.com>
Received: from exprod7mx224.postini.com [64.18.2.151] by mail74.safesecureweb.com with SMTP;
   Fri, 10 Jul 2009 11:11:23 -0400
Received: from source ([71.235.188.88]) by exprod7mx224.postini.com ([64.18.6.14]) with SMTP;
        Fri, 10 Jul 2009 11:11:02 EDT
Received: from PAZZO3 ([127.0.0.1]) by PAZZO3 with Microsoft SMTPSVC(6.0.2600.5512);
         Fri, 10 Jul 2009 11:10:59 -0400
Date: Fri, 10 Jul 2009 11:10:59 -0400
From: alicia@localhost.com
Subject: Testing HTML email from localhost
To: alicia@wallewu.com
cc: alicia@orangemooncreations.com
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="PHP-alt-887f3219a5f4539fb962dd1f6d7fef62"
Return-Path: alicia@localhost.com
Message-ID: <PAZZO3xXvrTrT37OjCV0000000b@PAZZO3>
X-OriginalArrivalTime: 10 Jul 2009 15:10:59.0546 (UTC) FILETIME=[A1C493A0:01CA0170]
X-pstn-neptune: 0/0/0.00/0
X-pstn-levels: (S:40.89761/99.90000 CV:99.9000 FC:95.5390 LC:95.5390 R:95.9108 P:95.9108 M:97.0282 C:98.6951 )
X-pstn-settings: 4 (1.5000:1.5000) s cv gt3 gt2 gt1 r p m c
X-pstn-addresses: from <alicia@localhost.com> [db-null]
X-SmarterMail-Spam: Bayesian Filtering, SpamAssassin -4 [raw: -4], SPF_Fail, DK_None
X-SmarterMail-TotalSpamWeight: 36
X-Antivirus: AVG for E-mail 8.5.375 [270.13.9/2229]

Open in new window

Can you test from a server instead of "localhost" - if you have a hosting account somewhere, they almost certainly have the mail() command working right.
I think the mail command is working fine, since I am receiving the email. The problem is that my email client isn't recognizing it as an HTML email, so I think that's because there's something wrong in the way the boundary thing - and maybe the content-transfer-encoding thing, are being set.

I will try on a server and post back.
OK - copied the page to one of my hosted accounts (on HostMySite.com) - same issue.
I suggest that you use a different method for your html mail, for instance the PEAR mail package.

http://pear.php.net/package/Mail

PS: This question was about output bufferening, and it has been answered. You should accept the answer and open a new question regarding the html mail because this is a different issue.