Multi-part mime ARGHHHHHH!

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I think beating my head against a wall would be more productive than
trying to figure this out on my own!  I'm trying to create an HTML
formatted email message with two attachments using PHP's mail().  I'm
getting all kinds of wonderful garbage, non closely resembling my

Can one of you, far more knowledgeable than I, please tell me what I'm
doing wrong?



=============== My lame attempt at code ===============


//    Define addressing info
    $to = "";
    $subject = "Testing html email with attachments";

//    Define headers and boundaries
    $rndhash = md5(date('r', time()));

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

    $attachHeader = "Content-Disposition: attachment\n";
    $attachHeader .= "Content-Transfer-Encoding: base64\n\n";

    $mixBoundary = "--PHP-mixed-".$rndhash."\n\n";

//    Define Message Headers
    $headers = "From:\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-" .
$rndhash . "\"\n";
    $headers .= "Content-Transfer-Encoding: 7bit\n\n";

//    Create the Message bodies (text and html)
    $htmlContent = "<p>Here is some text in the message.</p><p>Here is
some more text.</p>\n";

//    Assemble the Message
    $body = "";

    //    Add an html version
    $body .= $htmlHeader;
    $body .= $htmlContent."\n";
    $body .= $mixBoundary;

    //    Add first attachment
    $attachment1 = chunk_split(base64_encode(file_get_contents
    $body .= "Content-Type: application/pdf; name=\"fw9.pdf\"\n";
    $body .= $attachHeader;
    $body .= $attachment1."\n";
    $body .= $mixBoundary;

    //    Add second attachment
    $attachment2 = chunk_split(base64_encode(file_get_contents
    $body .= "Content-Type: application/zip; name=\"\"\n";
    $body .= $attachHeader;
    $body .= $attachment2."\n";
    $body .= $mixBoundary;

//    Send the message
    $mail_sent = @mail( $to, $subject, $body, $headers );

//    if the message is sent successfully print "Mail sent". Otherwise
print "Mail failed"
    echo $mail_sent ? "Mail sent" : "Mail failed";


Re: Multi-part mime ARGHHHHHH!

Quoted text here. Click to load it

That's not right, for two reasons.  Sections are separated by a separator
line, followed by ONE end-of-line, followed by the headers for the next
section.  More importantly, the entire email is terminated by a line with
the separator line followed by two dashes and an end of line.

Quoted text here. Click to load it

$headers .= "MIME-Version: 1.0\n";

Quoted text here. Click to load it

The file name goes on the Content-Disposition header, not the Content-Type

Quoted text here. Click to load it

Tim Roberts,
Providenza & Boekelheide, Inc.

Re: Multi-part mime ARGHHHHHH!


on 02/28/2009 03:24 PM Mike said the following:
Quoted text here. Click to load it

It is hard to tell. Maybe you are getting the MIME format wrong somewhere.

Another thing is that you should not send HTML only messages, or else
some mail systems , like Hotmail for instance, will consider your
message SPAM.

You may want to take a look at this presentation that talks about
problems with sending e-mail messages:

It is mainly about this class that can correctly compose and send
messages compliant with the Internet mail standards. You are recommended
to use it instead of handcode your messages.


Manuel Lemos

Find and post PHP jobs /

PHP Classes - Free ready to use OOP components written in PHP /

Re: Multi-part mime ARGHHHHHH!

mlemos wrote:
Quoted text here. Click to load it


When are you going to start telling people this is YOUR junk site?  And
that your mimemessage class is pure shite.   There are many other
classes much better - like the phpmailer class Michael suggested.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline