Solved

Why is output buffering not working?

Posted on 2009-07-10
14
247 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
  • 7
  • 4
  • 3
14 Comments
 

Author Comment

by:alicia1234
Comment Utility
"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 250 total points
Comment Utility
Change line 36 from this:

<?

...to this:

<?php
0
 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
"<?" 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
 

Author Comment

by:alicia1234
Comment Utility
 <!--[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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
OK - copied the page to one of my hosted accounts (on HostMySite.com) - same issue.
0
 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now