DeveloperBarn Forums

DeveloperBarn

Programming & IT forum

Sending HTML email...

This is a discussion on Sending HTML email... within the PHP Development forums, part of the Programming & Scripting category; I'm nearing my wits end on this... I have a page setup to send an html email of a report ...

Go Back   DeveloperBarn Forums > Programming & Scripting > PHP Development

  #1  
Old April 8th, 2009, 01:21 PM
bryceowen's Avatar
Barn Regular
 
Join Date: Sep 2008
Location: Jacksonville, FL
Posts: 92
Rep Power: 2
bryceowen is on a distinguished road
Default Sending HTML email...

I'm nearing my wits end on this...

I have a page setup to send an html email of a report that is generated by a given date. This form works great, exactly as expected. Now I am trying to create a new form to send a simple html email to someone and I'm being stymied at every turn...

Here's the code of the page that works (the addresses have been changed to protect the innocent):
PHP Code:
<?PHP

if(isset($_POST["email"])){$to=$_POST["email"];}
if(isset(
$_POST["body"])){$body=$_POST["body"];}
if(isset(
$_POST["date"])){$date=$_POST["date"];}

$body=strip_tags($body'<table><tr><td><p><br><div>');
$subject "Sales report for $date";
$random_hash md5(date('r'time()));
$headers "From: example@example.com\r\nReply-To: example@example.com\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
ob_start();
?>
--PHP-alt-<?php echo $random_hash?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Your email does not support HTML formatted email.

--PHP-alt-<?PHP echo $random_hash?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<html>
<body>
<?PHP echo $body;?>
</body>
</html>

--PHP-alt-<?PHP echo $random_hash?>--
<?PHP
$message 
ob_get_clean();
mail$to$subject$message$headers );
echo 
"Message has been sent. Please use the back button on your browser to return to the report.";
?>
The $body is a table that is pulled from the report page. Now, I've replaced the <?PHP echo $body;?> line with something simple like <p>Hello world</p> and the email still sends fine. HOWEVER, when I try to create a new email page, I get nothing but the subject! Here's what I'm trying:
PHP Code:
<?PHP
$to
="my@email.com";
$subject "Test email";
$random_hash md5(date('r'time()));
$headers "From: example@example.com\r\nReply-To: example@example.com\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
ob_start();
?>
--PHP-alt-<?php echo $random_hash?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Your email does not support HTML formatted email.

--PHP-alt-<?PHP echo $random_hash?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<html>
<body>
<p>Hello, world!</p>
</body>
</html>

--PHP-alt-<?PHP echo $random_hash?>--
<?PHP
$message 
ob_get_clean();
mail$to$subject$message$headers );
echo 
$message;
?>
For some reason, this doesn't work! I copied the code from the above page and the only changes I made was to remove references to $body and set the $to variable.

Please help. I'm about to drop-kick my web server...

One more ridiculous question... Is there somewhere you need to authorize a page to send email? I don't remember ever having to do that... Or would the name of the file have anything to do with it? Or the directory in which it's stored?

Last edited by bryceowen; April 8th, 2009 at 01:34 PM.
Reply With Quote
  #2  
Old April 8th, 2009, 02:16 PM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,964
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

Why use an output buffer? Follow the HTML example here: PHP: mail - Manual
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

Reply With Quote
  #3  
Old April 8th, 2009, 03:58 PM
bryceowen's Avatar
Barn Regular
 
Join Date: Sep 2008
Location: Jacksonville, FL
Posts: 92
Rep Power: 2
bryceowen is on a distinguished road
Default

Well, I used the output buffer because that's what worked the first time I tried to get PHP to send HTML email. (Refer to: PHP: Sending Email (Text/HTML/Attachments))

I'm at a loss as to why it doesn't work when I copy the EXACT SAME code into another file and load it...

I did go back and try it the way it says in the manual and I finally got it to send, but I'd still like the multi-part option to work right. Some of the folks who get these messages read them on their smartphones and they don't display HTML email...
Reply With Quote
  #4  
Old April 8th, 2009, 07:28 PM
bryceowen's Avatar
Barn Regular
 
Join Date: Sep 2008
Location: Jacksonville, FL
Posts: 92
Rep Power: 2
bryceowen is on a distinguished road
Default

O.K. I figured out how to get the multi-part done. Just posting here for future use.

I'm still not able to get it to work the way my original page works, but this accomplishes the same thing.
PHP Code:
$random_hash=md5(date('r',time()));
$subject="Subject goes here.";
$to="example@example.com";
$headers="From: example@example.com\r\nReply-To: example@example.com\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";

$message='
--PHP-alt-'
.$random_hash.'
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Simple, plain-text message goes here.

--PHP-alt-'
.$random_hash.'
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<html>
<head>
<style type="text/css">
p{font-weight:bold;text-align:center;}
</style>
</head>
<body>
<p>Fancy HTML message would go here.</p>
</body>
</html>

--PHP-alt-'
.$random_hash.'--';
mail($to,$subject,$message,$headers); 
Reply With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > PHP Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Sending Emails Using ASP.NET richyrich .Net Code Samples 5 July 6th, 2009 04:21 AM
Reduce HTML File Size BLaaaaaaaaaarche HTML & CSS Help 1 February 19th, 2009 08:06 PM
How to insert variable html into php evdv PHP Development 1 January 14th, 2009 08:54 PM
Sending Email with ASP and CDOSYS jmurrayhead ASP Code Samples 1 November 13th, 2008 09:26 AM
Sending Email using CDOSYS & Classic ASP richyrich ASP Code Samples 0 March 17th, 2008 09:52 AM


All times are GMT -4. The time now is 06:34 PM.


Copyright ©2008-2010, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2