views:

196

answers:

1

Hi. I'm using the following code to send an email with attachments:

    $mime_boundary = "<<<--==+X[".md5(time())."]";

    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: multipart/mixed;\r\n";
    $headers .= " boundary=\"".$mime_boundary."\"";

    $message .= "This is a multi-part message in MIME format.\r\n\r\n";
    $message .= "--".$mime_boundary."\r\n";

    $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
    $message .= "Content-Transfer-Encoding: 7bit\r\n";
    $message .= "\r\n";
    $message .= "$message_body\r\n";
    $message .= "--".$mime_boundary."\r\n";

    foreach($attachments as $filename => $data)
    {
        $message .= "Content-Type: application/octet-stream;\r\n";
        $message .= " name=\"$filename\"\r\n";
        $message .= "Content-Transfer-Encoding: quoted-printable\r\n";
        $message .= "Content-Disposition: attachment;\r\n";
        $message .= " filename=\"$filename\"\r\n";
        $message .= "\r\n";
        $message .= chunk_split(base64_encode($data));
        $message .= "\r\n";
        $message .= "--".$mime_boundary."\r\n";
    }

    mail($email_address, $email_subject, $message, $headers);

Which works fine, except that an extra file is also attached (called "Part 1.4").

Is there a way to not have this added?

Cheers, Dan.

+3  A: 

IIRC the last part separator must be --something unique--, i.e. in your case

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

But mime mail is more or less a solved problem ( i.e. for an application developer it's boring when done correctly and reeeeally annoying when done wrong ;-) ). Do yourself a favor and use something like Swiftmailer or any other descend mailing library/class.

VolkerK
That's great! Works perfectly now. Thankyou :-)
Dan
Also just given the Swiftmailer a go. Very nice and simple to use. Thanks again.
Dan
Brilliant :) Though mime mail isn't exactly rocket science it has many tricky maybe even tedious aspects to it. I always advice to use a mailing library (unless you find emails so interesting that writing the best library there is becomes your new project/hobby - someone has to write those neat wrappers). Testing whether the email really arrives and is displayed correctly for the majority of all users is a task big enough for application developers - no need to also fiddle with the internals using string concatenation =]
VolkerK