我真的需要以下腳本的幫助。我有一個上傳表單,我想將上傳的文件作爲電子郵件附件發送。我在網上發現了一個片段,但是當我發送郵件時,我的常規文本被髮送並且附件被粘貼爲電子郵件中的神祕字符。PHP:發送帶有上傳圖片的文本郵件作爲附件
if (isset($_POST['submit'])):
/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
if (file($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
$headers = "From: [email protected]" . "\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .= "Reply-To: " . $mail . " \r\n";
$subject = "Errormessage: " . $error;
$message .= "this is some text that get's sent properly!";
// Send Error
$success = mail($mail, $subject, $message, $headers); ?>
任何想法我在做什麼錯在這裏?我很樂意得到一些幫助。
預先感謝您。
最開始調試這樣的問題是使用由正常的電子郵件客戶端發送郵件,並比較其結構(「查看消息源」)到通過php發送的消息的結構。 – arkascha 2014-08-31 13:11:32
嘗試將'Content-Disposition:attachment'標頭添加到附件的標題中。 – Fracsi 2014-08-31 13:21:00