+ Reply to Thread
Results 1 to 6 of 6

Thread: Email message: output buffer or variable

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

    Email message: output buffer or variable

    I've run into a little problem with sending email in PHP. I've been creating the body of the message using ob_get_clean() and it works just fine for me. However, it was asked of me why I wasn't just writing the message directly to the variable. I thought I'd give it a try and I'm getting a weird result.

    Using the ob_get method, my email creates and sends just fine (multi-part with attachments), but if I try to write the body directly into the variable, the email message that comes through appears as plain text (it sends the raw information without actually formatting or anything).

    Here's a sample message I test. It sends just fine and comes across as it should:
    PHP Code:
    <?PHP
    $random_hash
    =md5(date('r',time()));
    $to="you@your.domain";
    $subject="Test message.";
    $headers="From: you@your.domain\r\nReply-To: you@your.domain";
    $headers.="\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

    //Message collected as output buffer
    ob_start();
    ?>
    --PHP-mixed-<?PHP echo $random_hash;?>

    Content-Type: multipart/alternative; boundary="PHP-alt-<?PHP echo $random_hash;?>"

    --PHP-alt-<?PHP echo $random_hash;?>

    Content-Type: text/plain; charset="iso-8859-1"
    Content-Transfer-Encoding: 7bit

    This is a simple text message.

    --PHP-alt-<?PHP echo $random_hash;?>

    Content-Type: text/html; charset="iso-8859-1"
    Content-Transfer-Encoding: 7bit

    <html><body><div style="text-align:center;">This is a complex HTML message.</div></body></html>

    --PHP-alt-<?PHP echo $random_hash;?>--

    --PHP-mixed-<?PHP echo $random_hash;?>--
    <?PHP
    $message
    =ob_get_clean();
    //End of message

    $mail_sent=@mail($to,$subject,$message,$headers);
    echo 
    $mail_sent "Message sent." "Great, you broke it.";
    ?>
    And here's the same message, but with the $message written as a variable directly rather than pulled from the ob:
    PHP Code:
    <?PHP
    $random_hash
    =md5(date('r',time()));
    $to="you@your.domain";
    $subject="Test message.";
    $headers="From: you@your.domain\r\nReply-To: you@your.domain";
    $headers.="\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

    //Message created as variable
    $message="--PHP-mixed-$random_hash

    Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"

    --PHP-alt-$random_hash

    Content-Type: text/plain; charset=\"iso-8859-1\"
    Content-Transfer-Encoding: 7bit

    This is a simple text message.

    --PHP-alt-$random_hash

    Content-Type: text/html; charset=\"iso-8859-1\"
    Content-Transfer-Encoding: 7bit

    <html><body><div style=\"text-align:center;\">This is a complex HTML message.</div></body></html>

    --PHP-alt-$random_hash--

    --PHP-mixed-$random_hash--"
    ;
    //End of message

    $mail_sent=@mail($to,$subject,$message,$headers);
    echo 
    $mail_sent "Message sent." "Great, you broke it.";
    ?>
    If you run them, you'll see what I mean by raw message.

    Could someone tell me what I'm doing wrong in the latter file?

    ***EDIT***
    Checking the two $message variables, I found they are both string-type and, when echoed, APPEAR identical, but strlen of the $message from the broken file is two characters longer than the first (working) one. I don't know where those two characters are coming from. As I said, they APPEAR identical when echoed...
    Last edited by bryceowen; September 2nd, 2009 at 01:46 PM. Reason: Odd finding...

  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
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    It looks like you're missing the text/html Content-Type in $headers, i.e.:
    Code:
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n".
    
    jmurrayhead
    If you agree, give me rep.
    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
    93
    Rep Power
    4

    The message isn't just HTML, though. It's both plain and HTML.

    The headers show "multipart/mixed" and in the message, the boundaries for the text/plan and text/html appear around their respective areas. (The format I use in this email example is the one I use for sending attachments, but it works just as well for text only.)

  4. #4
    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
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Gotcha...

    I found the following example. Try to plug in your requirements to this. However, this uses the output buffer...and I see nothing wrong with that:

    PHP Code:
    <?php 
    //define the receiver of the email 
    $to 'youraddress@example.com'
    //define the subject of the email 
    $subject 'Test email with attachment'
    //create a boundary string. It must be unique 
    //so we use the MD5 algorithm to generate a random hash 
    $random_hash md5(date('r'time())); 
    //define the headers we want passed. Note that they are separated with \r\n 
    $headers "From: webmaster@example.com\r\nReply-To: webmaster@example.com"
    //add boundary string and mime type specification 
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""
    //read the atachment file contents into a string,
    //encode it with MIME base64,
    //and split it into smaller chunks
    $attachment chunk_split(base64_encode(file_get_contents('attachment.zip'))); 
    //define the body of the message. 
    ob_start(); //Turn on output buffering 
    ?> 
    --PHP-mixed-<?php echo $random_hash?>  
    Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash?>
    --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-mixed-<?php echo $random_hash?>  
    Content-Type: application/zip; name="attachment.zip"  
    Content-Transfer-Encoding: base64  
    Content-Disposition: attachment  
    <?php echo $attachment?> 
    --PHP-mixed-<?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"
    ?>
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


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

    That's almost exactly what I have in my first file (sans the attachment part).

    I guess the short answer is that there's no way to send email properly formatted without using the output buffer.

  6. #6
    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
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    I'm not completely sure...but every decent example I've found uses it. Like I said, I really don't see anything wrong with that.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


+ Reply to Thread

Similar Threads

  1. Format MRP Output
    By BLaaaaaaaaaarche in forum ASP Development
    Replies: 22
    Last Post: February 13th, 2009, 02:25 PM
  2. Output from Database
    By uk26 in forum ASP Development
    Replies: 23
    Last Post: February 12th, 2009, 02:43 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. Session variable not querying??
    By Centurion in forum ASP Development
    Replies: 7
    Last Post: December 3rd, 2008, 11:35 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