2013-11-26 70 views
0

雖然知道更好的解決方案,但我使用手動PHP腳本將文件附加到要通過電子郵件發送的郵件。該文件正確連接,我甚至可以看到該消息,但由於某種原因,原始消息($msg)被摺疊!所有那些\n\r的我都消失了。有任何想法嗎??PHP - 帶附件的電子郵件崩潰原始郵件

這裏是我的代碼:

// construct message 
$msg = "Beginning of message \n -------------------- \n\n"; 
$msg .= "some logic is added to the message using a loop with \r\n in between each item on the list.\r\n"; 
$msg .= "--\n The end of the message."; 

// handle file upload 
$attachment = $_FILES['uploaded']['tmp_name']; 
$att_type = $_FILES['uploaded']['type']; 
$att_name = $_FILES['uploaded']['name']; 

if (is_uploaded_file($attachment)) { 
    // read the file to be attached ('rb' = read binary) 
    $file = fopen($attachment, 'rb'); 
    $data = fread($file, filesize($attachment)); 
    fclose($file); 

    // generate a boundary string 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x[$semi_rand]x"; 

    // add the headers for a file attachment 
    $headers .= "MIME-Version: 1.0\r\n" . 
    "Content-Type: multipart/mixed; boundary=\"{$mime_boundary}\"\r\n\r\n"; 

    // MESSAGE GETS WRITTEN HERE 
    // add a multipart boundary above the plain message 
    $message = "This is a multi-part message in MIME format.\r\n" . 
    "--{$mime_boundary}\r\n" . 
    "Content-Type: text/html; charset=\"iso-8859-1\"\r\n\r\n"; 
    $message .= $msg . "\r\n\r\n"; 

    // add file attachment to the message 
    $message .= "\r\n--{$mime_boundary}\r\n" . 
    "Content-Type: {$att_type};\r\n" . 
    " name=\"{$att_name}\"\n" . 
    "Content-Disposition: attachment;" . 
    " filename=\"{$att_name}\"\r\n\r\n" . 
    $data . "\r\n" . 
    "--{$mime_boundary}--\n"; 
} 
else { 
    $message = $msg; 
} 
$success = mail($to, $subject, $message, $headers); 

當文件被上載,該消息在它換行符到達。但是,當文件附加它崩潰。爲什麼?任何幫助感謝!否則,這個腳本的作品

回答

0

看起來像是通過使用Content-Type: text/html告訴電子郵件客戶端它是HTML電子郵件。大多數電子郵件客戶端將通過純文本版本顯示電子郵件的HTML內容。這意味着\r\n將不起作用。您需要在HTML版本中使用<br />才能獲得換行符。

+0

哦,太棒了!而已。我只是將它改爲text/plain並且它工作。現在這一切都有意義。謝謝! – Chemist