+ Reply to Thread
Results 1 to 4 of 4

Thread: Sending HTML email...

  1. #1
    Barn Regular bryceowen is on a distinguished road bryceowen's Avatar
    Join Date
    Sep 2008
    Location
    Jacksonville, FL
    Posts
    92
    Rep Power
    2

    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.

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Why use an output buffer? Follow the HTML example here: PHP: mail - Manual
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  3. #3
    Barn Regular bryceowen is on a distinguished road bryceowen's Avatar
    Join Date
    Sep 2008
    Location
    Jacksonville, FL
    Posts
    92
    Rep Power
    2

    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...

  4. #4
    Barn Regular bryceowen is on a distinguished road bryceowen's Avatar
    Join Date
    Sep 2008
    Location
    Jacksonville, FL
    Posts
    92
    Rep Power
    2

    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 to Thread

Similar Threads

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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO