?
Solved

Why is output buffering not working?

Posted on 2009-07-10
14
Medium Priority
?
257 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:alicia1234
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
14 Comments
 

Author Comment

by:alicia1234
ID: 24823849
"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

0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 1000 total points
ID: 24823878
Change line 36 from this:

<?

...to this:

<?php
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24823911
"<?" 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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:alicia1234
ID: 24823957
 <!--[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.

0
 

Author Comment

by:alicia1234
ID: 24823992
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.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24824000
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
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24824075
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

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24824174
>> 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."\"";
0
 

Author Comment

by:alicia1234
ID: 24824177
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

0
 

Author Comment

by:alicia1234
ID: 24824237
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

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24824272
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.
0
 

Author Comment

by:alicia1234
ID: 24824342
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.
0
 

Author Comment

by:alicia1234
ID: 24824383
OK - copied the page to one of my hosted accounts (on HostMySite.com) - same issue.
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24824559
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.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question