2012-02-05 75 views
-1

我有一个函数可以发送带有多个word文档的电子邮件作为附件。唯一的问题是,无论我发送多少文件,电子邮件中只显示1个,而且显示为损坏。文件路径名是正确的,我已经测试过这个,但是当我尝试从电子邮件中打开文档时,Microsoft word抱怨文件已损坏。有人请告诉我我做错了什么?在PHP中发送多个word文档作为附件

function mail_attachment_multiple($to, $subject, $message, $attachment, $from){//sends email as an attachment. attachment parameter is the path to file 

$fileatt_type = "application/msword"; // File Type 
$email_from = $from; // Who the email is from 
$email_subject = $subject; // The Subject of the email 
$email_txt = $message; // Message that the email has in it 
$email_to = $to; // Who the email is to 
$headers = "From: ".$email_from; 
$msg_txt="\n\n You have recieved a new attachment message from $from"; 

$email_message = ""; 

//loop through array: $key = filename; $value = filenamePATH 
foreach($attachment as $key => $value){ 
    $fileatt_name = $key; //name of file 
    $fileatt_path = $value; // file path (http://www.example.com/filePath) 

    $file = fopen($fileatt_path,'rb'); 
     $temp = get_headers($fileatt_path, 1); 
    $file_size = $temp['Content-Length'];   
    $data = fread($file, $file_size); //filesize($fileatt_path) 
    fclose($file); 

    $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_txt .= $msg_txt; 
    $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_txt . "\n\n"; 
    $data[$key] = chunk_split(base64_encode($data));   

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

return @mail($email_to, $email_subject, $email_message, $headers);?> 

感谢您的帮助!

+2

不需要。自己修复此问题。当Swiftmailer或PHPmailer有良好的工作选择时,没有人应该修补此问题。保持意大利面代码是你的决定。 – mario 2012-02-05 02:37:22

回答

1

你应该真的使用邮件库这种事情。我强烈建议SwiftMailer

+0

我会研究它。谢谢!你以前使用过PHPmailer吗?如果是这样,为什么你推荐SwiftMailer而不是PHPmailer? – Johny 2012-02-05 02:47:49

+0

@Johny我以前没有看过PHPMailer。我推荐SwiftMailer,因为它部分由Fabien Potencier/Senio Labs维护,负责Symfony和Doctrine。基于此,我知道SwiftMailer将会有一个体面的代码库和单元测试。 – Cobby 2012-02-05 03:04:13