使用PHP脚本发送包含多个附件的电子邮件

时间:2014-04-11 10:11:16

标签: php email-attachments

使用php发送电子邮件时遇到小问题。我正在尝试发送包含多个附件的电子邮件。此代码以单个附件运行。请告诉我如何使用此代码附加多个附件,..

   <?php
    if(isset($_POST['EMAIL']))
    {
    $to = $_POST['EMAIL'];
    $fromEmail = $_POST['frmEMAIL']; 

     $subject = $_POST['SUBJECT']; 
     $message = $_POST['MESSAGE'];
     $frmCCEMAIL = $_POST["CC"];
    $frmBCCEMAIL = $_POST["BCC"];

 /* GET File Variables */ 
 $tmpName = $_FILES['attachment']['tmp_name']; 
 $fileType = $_FILES['attachment']['type']; 
  $fileName = $_FILES['attachment']['name']; 

 /* Start of headers */ 
  $headers = "From: $fromEmail "; 



   if (file($tmpName)) { 
   /* Reading file ('rb' = read binary)  */
  $file = fopen($tmpName,'rb'); 
  $data = fread($file,filesize($tmpName)); 
  fclose($file); 

  /* a boundary string */
  $randomVal = md5(time()); 
  $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 

  /* Header for File Attachment */
   $header .= "Cc:".$frmCCEMAIL." \n";
   $header .= "Bcc:".$frmBCCEMAIL." \n";
   $headers .= "\nMIME-Version: 1.0\n"; 
   $headers .= "Content-Type: multipart/mixed;\n" ;
   $headers .= " boundary=\"{$mimeBoundary}\""; 

   /* Multipart Boundary above message */
    $message = "This is a multi-part message in MIME format.\n\n" . 
     "--{$mimeBoundary}\n" . 
    "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
    "Content-Transfer-Encoding: 7bit\n\n" . 
    $message . "\n\n"; 

   /* Encoding file data */
    $data = chunk_split(base64_encode($data)); 

   /* Adding attchment-file to message*/
   $message .= "--{$mimeBoundary}\n" . 
    "Content-Type: {$fileType};\n" . 
    " name=\"{$fileName}\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . 
    $data . "\n\n" . 
    "--{$mimeBoundary}--\n"; 
    } 

       $flgchk = mail ("$to", "$subject", "$message", "$headers", "$header"); 

      if($flgchk)
   {
      echo "<script>alert('EMAIL sent successfully.')</script>"; 
    }
   else
    {
  echo "<script>alert('EMAIL could not be sent.')</script>"; 
    }

   unset($value); // break the reference with the last element
   header("refresh:0;url=email.php");
    }

   ob_flush();
      ?>

0 个答案:

没有答案