2010-01-08 106 views
3

我使用以下代碼發送帶附件的電子郵件,但正確的文件未附加郵件。如何使用PHP發送帶附件的電子郵件?

$UnidID = $_COOKIE['UniqueID']; 
$guid = $_COOKIE['guid']; 
$target_path = "userdata/".$UniqueID."/".$iGuid."/Outputs"; 
$fname = getpathmail($UnidID,$guid); 
$target_path = $target_path.$filname; 

$fileatt_type = "application/fbf"; // File Type 
$fileatt_name = $fname; 
$data = $target_path; 
$email_from = "[email protected]"; 
$email_subject = "EHP/PPP process"; 
$email_message = "Processed result for EHP/PPP processing"; 

$email_to = $_GET['Email'] ; 

$headers = "From: ".$email_from; 

$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

$headers .= "\nMIME-Version: 1.0\n" . 
"Content-Type: multipart/mixed;\n" . 
" boundary=\"{$mime_boundary}\""; 

$email_message .= "This is a multi-part message in MIME format.\n\n" . 
"--{$mime_boundary}\n" . 
"Content-Type:text/html; charset=\"iso-8859-1\"\n" . 
"Content-Transfer-Encoding: 7bit\n\n" . 
$email_message .= "\n\n"; 

$data = chunk_split(base64_encode($data)); 

$email_message .= "--{$mime_boundary}\n" . 
"Content-Type: {$fileatt_type};\n" . 
" name=\"{$fileatt_name}\"\n" . 
"Content-Transfer-Encoding: base64\n\n" . 
$data .= "\n\n" . 
"--{$mime_boundary}--\n"; 

$ok = @mail($email_to, $email_subject, $email_message, $headers); 
+0

你或你可以嘗試使用類似[PHPMailer](http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/)的類嗎? – Tim 2010-01-08 11:06:05

+0

你可以在這裏找到一個完整的工作功能: http://www.barattalo.it/2010/01/10/sending-emails-with-attachment-and-html-with-php/ – Pons 2010-01-11 13:22:33

回答

2

你需要把你所有的連鎖信息到$email_message

$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

$headers = "From: ".$email_from; 
$headers .= "\nMIME-Version: 1.0\n" . 
"Content-Type: multipart/mixed;\n" . 
" boundary=\"{$mime_boundary}\""; 

$email_message .= "This is a multi-part message in MIME format.\n\n" . 
"--{$mime_boundary}\n" . 
"Content-Type:text/html; charset=\"iso-8859-1\"\n" . 
"Content-Transfer-Encoding: 7bit\n\n" . 
"--{$mime_boundary}\n" . 
"Content-Type: {$fileatt_type};\n" . 
" name=\"{$fileatt_name}\"\n" . 
"Content-Transfer-Encoding: base64\n\n" . 
chunk_split(base64_encode($data)) . 
"--{$mime_boundary}--\n"; 
3

我建議使用庫發送電子郵件,因爲它會處理所有的標題相關的東西。看看Zend_Mail。發送附件是那麼容易,因爲

$mail = new Zend_Mail(); 

$at = $mail->createAttachment($myImage); 
$at->type  = 'image/gif'; 
$at->disposition = Zend_Mime::DISPOSITION_INLINE; 
$at->encoding = Zend_Mime::ENCODING_8BIT; 
$at->filename = 'test.gif'; 

$mail->send(); 
+1

是的,使用郵件程序你的框架提供的類。無需重新發明輪子並添加更多需要維護的代碼。 – Sri 2010-01-08 11:07:53

-1

你應該給你的附件文件如:

"Content-Disposition: attachment; filename='/path/to/the/file/filename'"; 
+0

它說*文件名*而不是*文件路徑*。 – Gumbo 2010-01-08 11:17:55

+0

屬性名稱是文件名,但您需要指定文件的完整路徑,否則將如何知道在何處拾取附加文件。 – RahulJ 2010-01-11 09:51:24

+0

-1這不會創建任何電子郵件附件。 – hakre 2012-10-14 21:46:44

0

可能是你在你的代碼錯過了這個

... 

$file = fopen($data,'rb'); 
//saves content in $data itself 
$data = fread($file,filesize($data)); 
fclose($file); 

... 

應該工作沒有帶自己執行。 試一試

相關問題