Andrew Rumm
Andrew Rumm

Reputation: 1278

php mail: attachments errors

people!

I've gotta send messages with attachments. So i've got some code based on some examples from internet:

$path = $_SERVER['DOCUMENT_ROOT'] . ATTACHMENT_DIR . DS;
$files = array('filename1.ext', 'filename2.ext');
$EOL = "\r\n"; // "\n";

$to = "[email protected]";
$from = '=?UTF-8?B?' . base64_encode($_POST['name']) . '?='  . " <" . $_POST['email'] . ">";
$subject = "subject";
$subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';
$message = $_POST['message'];

$mime_boundary = md5(uniqid(time()));

$headers .= "From: $from" .
    "{$EOL}MIME-Version: 1.0{$EOL}" .
        "Content-Type: multipart/mixed; boundary=\"{$mime_boundary}\"{$EOL}{$EOL}" .
        "This is a multi-part message in MIME format.{$EOL}" .
        "-–{$mime_boundary}{$EOL}" .
        "Content-Type: text/plain; charset=UTF-8{$EOL}" .
        "Content-Transfer-Encoding: 8bit{$EOL}{$EOL}" .
        $message . "{$EOL}{$EOL}";

    // preparing attachments
    for ($x = 0; $x < count($files); $x++) {
        $file = fopen($path . $files[$x], "rb");
        $data = fread($file, filesize($path . $files[$x]));
        fclose($file);
        $data = chunk_split(base64_encode($data), 72, $EOL);
        $headers .= "-–{$mime_boundary}{$EOL}" .
            "Content-Type: application/octet-stream; name=\"$files[$x]\"{$EOL}" .
            "Content-Transfer-Encoding: base64{$EOL}{$EOL}" .
            "Content-Disposition: attachment; filename=\"$files[$x]\"{$EOL}" .
            $data . "{$EOL}";
}
$headers .= "--{$mime_boundary}--{$EOL}";

$result = @mail($to, $subject, "", $headers);

It works, but something is going wrong. This code generates:

To: [email protected]
Subject: =?UTF-8?B?c3ViamVjdA==?=
From: =?UTF-8?B?Sm9obiBEb2U=?= <[email protected]>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="2dd88e9ef3ae338d9e93bc8448a74093"

This is a multi-part message in MIME format.
-–2dd88e9ef3ae338d9e93bc8448a74093
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

test message

-–2dd88e9ef3ae338d9e93bc8448a74093
Content-Type: application/octet-stream; name="filename1.ext"
Content-Transfer-Encoding: base64

Content-Disposition: attachment; filename="filename1.ext"
R0lGODlhMgAyAPf/ALsoJpcZFeG8wZsoJb+NiKZDQMV6eKgYFqpJR6lWVdKtq6oeHP7//9a0
[cutted]
Qx66WAxbFOMNs8gDJAohCr5Svd8mUEgM7oAI7HghBRMJra6XEhAAOw==

-–2dd88e9ef3ae338d9e93bc8448a74093
Content-Type: application/octet-stream; name="filename2.ext"
Content-Transfer-Encoding: base64

Content-Disposition: attachment; filename="filename2.ext"
R0lGODlhMgAyAPf/ALsoJpcZFeG8wZsoJb+NiKZDQMV6eKgYFqpJR6lWVdKtq6oeHP7//9a0
[cutted]
Qx66WAxbFOMNs8gDJAohCr5Svd8mUEgM7oAI7HghBRMJra6XEhAAOw==

--2dd88e9ef3ae338d9e93bc8448a74093--

Message recieved without text or any attachments. Please, help me find suitable solution!

Upvotes: 0

Views: 443

Answers (2)

Drazisil
Drazisil

Reputation: 3343

I'm blaming the base64_encode.

Try the code at http://www.zedwood.com/article/126/php-mail-function-with-attachments instead and see if that works better.

Upvotes: 1

Kristen Waite
Kristen Waite

Reputation: 1465

Any particular reason you're not using Mail_Mime?

Upvotes: 0

Related Questions