2013-03-15 226 views
7

我试图在php中实现邮件功能,其工作正常,单个附件,但问题是当我试图发送多个附件,它不工作。我使用PHP邮件()函数发送电子邮件,我试图附上一个PDF和一个图像文件。如果PDF附加,则图像不会附加,如果图像附加,则PDF将不附加。任何想法,我做错了?php:电子邮件发送失败,多个附件

$header  .= 'From: test <[email protected]>' . "\r\n";  
$header  .= "MIME-Version: 1.0\r\n"; 
$file   = '1.png' 
$displayname = '1.png'; 
$file_size  = filesize($file); 
$handle  = fopen($file, "r"); 
$content  = fread($handle, $file_size); 
fclose($handle); 
$content  = chunk_split(base64_encode($content)); 
$uid   = md5(uniqid(time())); 
$name   = basename($file); 

$filepdf  = '1.pdf' 
$displaynamepdf= '1.pdf'; 
$file_sizepdf = filesize($filepdf); 
$handlepdf  = fopen($filepdf, "r"); 
$contentpdf = fread($handlepdf, $file_sizepdf); 
fclose($handlepdf); 
$contentpdf = chunk_split(base64_encode($contentpdf)); 
$name   = basename($file); 

$header  .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; 
$header  .= "This is a multi-part message in MIME format.\r\n"; 
$header  .= "--".$uid."\r\n"; 
$header  .= "Content-type:text/plain; charset=iso-8859-1\r\n"; 
$header  .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
$header  .= $message."\r\n\r\n"; 
$header  .= "--".$uid."\r\n"; 
$header  .= "Content-Type: application/octet-stream; name=\"".$displayname."\"\r\n"; // use different content types here 
$header  .= "Content-Transfer-Encoding: base64\r\n"; 
$header  .= "Content-Disposition: attachment; filename=\"".$displayname."\"\r\n\r\n"; 
$header  .= $content."\r\n\r\n"; 

$header  .= "Content-Type: application/octet-stream; name=\"".$displaynamepdf."\"\r\n"; // use different contentpdf types here 
$header  .= "Content-Transfer-Encoding: base64\r\n"; 
$header  .= "Content-Disposition: attachment; filename=\"".$displaynamepdf."\"\r\n\r\n"; 
$header  .= $contentpdf."\r\n\r\n"; 
$header  .= "--".$uid."--"; 

if (mail($to, $subject, "", $header)) { 
    return 'sent'; 
} else { 
    return 'not sent'; 
} 
+6

我的2¢:尝试使用[PHPMailer](http://phpmailer.worxware.com/)或[swiftmailer](http://swiftmailer.org/)。 – 2013-03-15 06:38:44

+1

是的,如果可以的话,可以使用图书馆 - 有很多问题会让很多聪明人花费大量时间弄清楚。我喜欢PHPMailer。该问题可能较小,并取决于您的SMTP服务器类型。图书馆会处理这些问题。 – 2013-03-15 06:40:07

+1

Mihai已经触及了这一点,但是对于任何真正的项目,php'mail()通常是不够的。我建议PHPMailer。它需要5分钟设置,而且非常强大。我不推荐任何PEAR邮件模块。 – 2013-03-15 06:45:12

回答

1
+1

PHPMailer是最好的选择和易于实施也谢谢 – user2172726 2013-03-15 12:06:05

+0

@ user2172726欢迎!我们在这里互相帮助....非常感谢Stack Overflow !!! – 2013-03-15 12:08:39

0

您有$file$filepdf都设置为'1.png'。设置$filepdf="1.pdf",以便标题的其余部分是正确的。

0

这里是我已经创造了多个附件的功能,在邮件功能多个附件的代码..

。并检查这一点。

/* 
    * Function Name: sentMail 
    * Scope  : Sent Mail Function with CC , BCC and Attachment files.. 
    * Input  : Recipient Name   => $recipientName // (Required) 
    *    Recipient MailId(Many) => $recipientMailId // (Required & Array Format) 
    *    Subject Content   => $subjectContent // (Required) 
    *    Message Content   => $messageContent // (Required) 
    *    Sender MailId    => $senderMailId // (Required) 
    *    Sender Name(Many)   => $senderName  // (Required) 
    *    Recipient CC MailId  => $recipientCCMailId //(Optional & Array Format) 
    *    Recipient BCC MailId  => $recipientBCCMailId //(Optional & Array Format) 
    *    Attachment Files   => $attachmentFiles  //(Optional & Array Format) 
    * Output  : Sent mail to the Recipient. 
    */ 
    public function sentMail($recipientName,$recipientMailId,$subjectContent,$messageContent,$senderName,$senderMailId,$recipientCCMailId,$recipientBCCMailId,$attachmentFiles) 
    { 
     try 
     { 
      /*Get the Mulitple Recipient CC MailId*/ 
      if(isset($recipientCCMailId)){ 
       if(count($recipientCCMailId)>1){ 
        $recipientCCMailId = implode(',',$recipientCCMailId); 
       } 
       else{ 
        $recipientCCMailId = $recipientCCMailId[0]; 
       } 
      } 

      /*Get the Mulitple Recipient BCC MailId*/ 
      if(isset($recipientBCCMailId)){ 
       if(count($recipientBCCMailId)>1){ 
        $recipientBCCMailId = implode(',',$recipientBCCMailId); 
       } 
       else{ 
        $recipientBCCMailId = $recipientBCCMailId[0]; 
       } 
      } 

      /*** Mail Contents Starts ***/ 
       $subj = $subjectContent; 
       $msg =""; 

       /*Get the Mulitple Recipient BCC MailId*/ 
       if(count($recipientMailId)>1){ 
        $recipientMailId = implode(',',$recipientMailId); 
        $msg .="Dear, <b>All</b>\r <br>"; 
       } 
       else{ 
        $recipientMailId = $recipientMailId[0]; 
        $msg .="Dear, <b>".ucwords($user_name)."</b> \r <br>"; 
       } 
       $msg .=$messageContent."\r <br><br>"; 
       $msg .="Thank you"."\r\n\n <br>"; 
       $msg .=$senderName."\r\n\n <br><br>"; 

       $headers =""; 

       /** Get the Mulitple Attachment files and Attachment Process Starts **/ 
       if(isset($attachmentFiles)){ 
        $headers .= "From: ".$senderMailId."\r\n"; 
        $headers .= "Cc: ".$recipientCCMailId."\r\n"; 
        $headers .= "Bcc: ".$recipientBCCMailId."\r\n"; 
        // boundary 
        $semi_rand = md5(time()); 
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

        // headers for attachment 
        $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
        // multipart boundary 
        $msg .= "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
        "Content-Transfer-Encoding: 7bit\n\n" . $msg . "\n\n"; 

        // preparing attachments 
        for($i=0;$i<count($attachmentFiles);$i++) 
        { 
         if(is_file($attachmentFiles[$i])) 
         { 
          $msg .= "--{$mime_boundary}\n"; 
          $fp  = @fopen($attachmentFiles[$i],"rb"); 
          $data = @fread($fp,filesize($attachmentFiles[$i])); 
             @fclose($fp); 
          $data = chunk_split(base64_encode($data)); 
          $msg .= "Content-Type: application/octet-stream; name=\"".basename($attachmentFiles[$i])."\"\n" . 
          "Content-Description: ".basename($attachmentFiles[$i])."\n" . 
          "Content-Disposition: attachment;\n" . " filename=\"".basename($attachmentFiles[$i])."\"; size=".filesize($attachmentFiles[$i]).";\n" . 
          "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
         } 
        } 
        $msg .= "--{$mime_boundary}--"; 
       } 
       /** Attachment Process Ends **/ 
       else{ 
        $headers .= "MIME-Version: 1.0" . "\r\n"; 
        $headers .= "Content-type:text/html;charset=utf-8" . "\r\n"; 
        //$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; 
        $headers .= "From: ".$senderMailId."\r\n"; 
        $headers .= "Cc: ".$recipientCCMailId."\r\n"; 
        $headers .= "Bcc: ".$recipientBCCMailId."\r\n"; 
       } 
      /*** Mail Contents Ends ***/  
      $sent_mail=mail($recipientMailId,$subj,$msg,$headers); 
      if($sent_mail) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
     catch (Exception $e) 
     { 
      getConnection('close'); 
      echo "Caught Exception:",$e->getMessage(); 
     } 
    } 
+0

这个可能对您有所帮助。 – MKV 2013-03-15 07:23:50

0

我正面临同样的问题。问题在于模拟边界。不要添加“ - ”。$ uid。“ - ”;在每个附件之后。在内容类型的附件之前添加边界就足够了。