2013-03-15 13 views
7

phpでメール機能を実装しようとしましたが、1つの添付ファイルで正常に動作しますが、複数の添付ファイルを送信しようとしたときに問題が発生しました。私は電子メールを送信するためにphp mail()関数を使用していますが、私は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

My 2¢:[PHPMailer](http://phpmailer.worxware.com/)または[swiftmailer](http://swiftmailer.org/)を使用してください。 –

+1

そうですね、可能であればライブラリを使用してください。賢明な人たちには多くの時間を要する多くの問題があります。私はPHPMailerが好きです。この問題は、ご使用のSMTPサーバーの種類に左右されます。ライブラリがこれらの問題を処理します。 –

+1

Mihaiはすでにこの点に触れていますが、PHPの 'mail()'は実際のプロジェクトでは通常不十分です。私はPHPMailerをお勧めします。セットアップには5分かかり、非常に強力です。 PEARメールモジュールはお勧めしません。 –

答えて

1
+1

PHPMailerは、最適なオプションであり、提案のおかげで実装も簡単です – user2172726

+0

@ user2172726ようこそ!私たちはお互いを助けるためにここにいる....だからスタックオーバーフローのおかげで! –

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

0

私は同じ問題に直面していました。問題はMIME境界にあります。 " - "を追加しないでください。$ uid。 " - ";すべての添付ファイルの後に。コンテンツタイプの添付ファイルの前に境界線を追加するだけで十分です。