Jump to content

Recommended Posts

I have been working on a newsletter system for a site i'm finishing and everything is working fine apart from when i'm trying to send html/plain text email

 

I found an article on this site http://www.tek-tips.com/faqs.cfm?fid=2681

 

I created my own function that gets post data from a form and creates the html.  The function then calls another function with the code in the article to send the email.

 

The email sends fine but instead of an html message it appears as plain text but with all the html code displayed and there is no plain text version (i have checked) so it looks like it just sent a failed html version.

 

What could be causing this problem?

 

Here is my code

function sendBothEmail($plain_text, $HTML, $from, $to, $subject) {
    $notice_text = "This is a multi-part message in MIME format.";
    
    $semi_rand = md5(time());
    $mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";
    $mime_boundary_header = chr(34) . $mime_boundary . chr(34);
        
    $body = "$notice_text
    
    --$mime_boundary
    Content-Type: text/plain; charset=us-ascii
    Content-Transfer-Encoding: 7bit
    
    $plain_text
    
    --$mime_boundary
    Content-Type: text/html; charset=us-ascii
    Content-Transfer-Encoding: 7bit
    
    $HTML
    
    --$mime_boundary--";
    
    if (@mail($to, $subject, $body,
        "From: " . $from . "\n" .
        "MIME-Version: 1.0\n" .
        "Content-Type: multipart/alternative;\n" .
        "     boundary=" . $mime_boundary_header))
        return "Correct";
    else
        return "Email NOT sent successfully!";
}

 

function send_newsletter() {

    $from = "Me <newsletters@mysite.com>";
    
    $plain_text = "This is a plain text email.\r\nIt is very cool.";
    $HTML = "<html><body>This is an <b style='color:purple'>HTML</b> text email.\r\nIt is very cool.</body></html>";
    
    $to = "You <you@yoursite.com>";
       
    $result = sendBothEmail($plain_text,$HTML,$from,$to,$subject);
    
    if($result != 'Correct') {
          return "Emails not sent";
       }
}

 

and here is what is output from that

 

   

    This is a multi-part message in MIME format.

       

        --==MULTIPART_BOUNDARY_f50866ab15be0d00424d1166c9c4fb5f

        Content-Type: text/plain; charset=us-ascii

        Content-Transfer-Encoding: 7bit

       

        This is a plain text email.

    It is very cool.

       

        --==MULTIPART_BOUNDARY_f50866ab15be0d00424d1166c9c4fb5f

        Content-Type: text/html; charset=us-ascii

        Content-Transfer-Encoding: 7bit

       

        <html><body>This is an <b style='color:purple'>HTML</b> text email.

    It is very cool.</body></html>

       

        --==MULTIPART_BOUNDARY_f50866ab15be0d00424d1166c9c4fb5f--

 

 

Your header is messed up!

it should be something like this

 

$boundary = uniqid(time());
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: Foo <foo@bar.com>\r\n";
$headers .= "Subject: Test mail\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";

$message = "This is a MIME encoded message."; 
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";
$message .= "This is the text/plain version.";

$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";
$message .= "This is the <b>text/html</b> version.";

$message .= "\r\n\r\n--" . $boundary . "--";

//mail('bar@foo.com', 'Test mail', $message, $headers);

 

Also Note that using the above you can probably get away with this

 //mail('', 'Test mail', $message, $headers);

as the header is complete

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.