2010-01-08 108 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); 

... 

应该工作没有带自己执行。 试一试

相关问题