2010-06-22 241 views
2

我似乎無法找到這個PHP函數的問題,我寫的應該發送附件的電子郵件。我一直在努力掙扎。php發送電子郵件附件

function myMail($to, $subject, $mail_msg, $filename, $contentType){ 

    $random_hash = md5(date('r', time())); 
    $headers = "From: [email protected]\r\nReply-To: ".$to; 
    $headers .= "\r\nContent-Type: ".$contentType. 
     "; boundary=\"PHP-mixed-".$random_hash."\""; 

    $attachment = chunk_split(base64_encode(file_get_contents($filename))); 
    ob_start(); 

    echo " 
--PHP-mixed-$random_hash 
Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\" 

--PHP-alt-$random_hash 
Content-Type: text/plain; charset=\"utf-8\" 
Content-Transfer-Encoding: 7bit 

$mail_msg 

--PHP-alt-$random_hash 

--PHP-mixed-$random_hash-- 
Content-Type: text/plain; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment 
--PHP-mixed-$random_hash-- 
"; 
    $message = ob_get_clean(); 
    $mail_sent = @mail($to, $subject, $message, $headers); 
    return $mail_sent ? "Mail sent" : "Mail failed"; 
} 

編輯的問題是,在郵件的消息與文件混合並作爲附件發送。

+1

你不是說什麼行不通。 – 2010-06-22 12:02:33

+0

包含整個輸出,包含頭文件(減去base64編碼文件) – Artefacto 2010-06-22 12:16:17

回答

6

Artefacto讓我看看輸出與更多的關注,我發現修復:

 
function myMail($to, $subject, $mail_msg, $filename, $contentType, $pathToFilename){ 
    $random_hash = md5(date('r', time())); 
    $headers = "From: [email protected]\r\nReply-To: ".$to; 
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
    $attachment = chunk_split(base64_encode(file_get_contents($pathToFilename))); 
    ob_start(); 
echo " 
--PHP-mixed-$random_hash 
Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\" 

--PHP-alt-$random_hash 
Content-Type: text/plain; charset=\"utf-8\" 
Content-Transfer-Encoding: 7bit 

$mail_msg 

--PHP-alt-$random_hash-- 

--PHP-mixed-$random_hash 
Content-Type: $contentType; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment 
--PHP-mixed-$random_hash-- 
"; 
$message = ob_get_clean(); 
$fh=fopen('log.txt','w'); 
fwrite($fh,$message); 
$mail_sent = @mail($to, $subject, $message, $headers); 
return $mail_sent ? "Mail sent" : "Mail failed"; 
} 
+0

一年後非常有用:-) – Neal 2011-04-13 18:35:48

3

除非您這樣做了解MIME郵件的內部運行情況,否則標準答案是使用郵件庫庫,如PHPMailerSwiftmailer,它們可以處理開箱即用的附件。

SwiftMailer示例如何附加文件是here

10

我剛剛看了幾封電子郵件,我注意到最終附件邊界以' - '結尾,而開始邊界標記沒有。在你的代碼,您有:

--PHP-mixed-$random_hash-- 
Content-Type: text/plain; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment 
--PHP-mixed-$random_hash-- 

也許應該是:

--PHP-mixed-$random_hash 
Content-Type: text/plain; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment 
--PHP-mixed-$random_hash-- 

看一看這裏的例子:

http://en.wikipedia.org/wiki/MIME#Multipart_messages

+0

+1,用於清理手頭腳本中的問題。 – 2010-06-22 12:56:46

+0

@Pekka謝謝,但也許我的努力是徒勞的。儘管得到了正確的解決方案,但看起來同樣的解決方案是獨立完成的:-( – Mike 2010-06-22 13:06:35

1

這些是我使用的標題,他們一直像魅力一樣工作。

$base = basename($_FILES['upload']['name']); 
$file = fopen($randname_path,'rb'); 
$size = filesize($randname_path); 
$data = fread($file,$size); 
fclose($file); 
$data = chunk_split(base64_encode($data)); 

//boundary 
$div = "==Multipart_Boundary_x".md5(time())."x"; 
//headers 
$head = "From: $from\n". 
     "MIME-Version: 1.0\n". 
     "Content-Type: multipart/mixed;\n". 
     " boundary=\"$div\""; 
//message 
$mess = "--$div\n". 
     "Content-Type: text/plain; charset=\"iso-8859-1\"\n". 
     "Content-Transfer-Encoding: 7bit\n\n". 
     "$message\n\n". 
     "--$div\n". 
     "Content-Type: application/octet-stream; name=\"$base\"\n". 
     "Content-Description: $base\n". 
     "Content-Disposition: attachment;\n". 
     " filename=\"$base\"; size=$size;\n". 
     "Content-Transfer-Encoding: base64\n\n". 
     "$data\n\n". 
     "--$div\n"; 
$return = "-f$from"; 

http://asdlog.com/Create_form_to_send_email_with_attachment