我必須發送帶有附件的消息。 所以我得根據從互聯網上一些例子一些代碼:
$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);
它的工作原理,但事情錯了。此代碼生成:
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--
收到的消息沒有文字或附件。請幫我找到合適的解決方案!
我會改變方法,並得到一個PHP郵件庫,如phpmailer http://phpmailer.worxware.com/然後你可以附加一個文件在一行。 – 2011-07-05 02:03:25
我已經想過了。我想找到一個更簡單的解決方案。 –
預製好的東西比你現有的還要難嗎? – 2011-07-05 04:58:14