上传的多个文件未附加到邮件

时间:2014-10-20 05:48:08

标签: php email file-upload email-attachments

我的文件上传的html表单是

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<input type="file" id="file" name="files[]" multiple="multiple"  />
<input type="submit" id="submit" name="submit" value="Submit"/>
</form>

邮件发送的php代码是:

$to = "my email id";
$subject ="Registration"; 
 $message ="<div style='border: 5px solid #B8B8B8;'><table border='0'cellspacing='0' cellpadding='10' > 
    <tr><td colspan='4'>Hi ,</td></tr>
        <tr><td colspan='4'>A new report</td></tr>
         <tr><td colspan='4'>Details are:</td></tr>
         <tr> <td><table  width='100%'>
         <tr><td><table border='1'cellspacing='0' cellpadding='0' width='98%'>
         <tr><td colspan='3' style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;' >Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: ".$sname."</td><td colspan='3'  style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;'>Email&nbsp;&nbsp;&nbsp;: ".$email."</td></tr>
         <tr> <td colspan='3' style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;'>Age&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: ".$age."</td><td colspan='3' style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;'>Date&nbsp;&nbsp;&nbsp;&nbsp;: ".$dt."</td></tr>
         <tr><td colspan='3' style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;'>Gender&nbsp;&nbsp;: ".$gender."</td><td colspan='3' style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;'>Mobile&nbsp;: ".$mob."</td></tr>
         <tr><td colspan='3' style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;'>Center&nbsp;&nbsp;&nbsp;: ".$lname."</td><td colspan='3'  style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;'>History&nbsp;: ".$history."</td></tr>
         <tr><td colspan='3' style='padding-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:14px;' height='20px;'>Address : ".$adrs."</td></tr></table></td></tr></table></td></tr>
         <tr><td colspan='3'>&nbsp;</td></tr>
          <tr><td><table width='98%'>
         <tr><td colspan='3' align='center'><b>(DEEG) REPORT</b></td></tr>
         <tr><td colspan='3'>&nbsp;</td></tr>
         <tr><td colspan='3'><b>Clinical Interpretation :</b></td></tr>
         <tr><td colspan='3' align='justify'> ".$clinic."</td></tr><br/>
         <tr><td colspan='3'><b>Report :</b></td></tr>
         <tr><td colspan='3' align='justify'> ".$report."</td></tr><br/>
         <tr><td colspan='3'><b>EKG :</b></td></tr>
         <tr><td colspan='3' align='justify'> ".$sumry."</td></tr><br/>
         <tr><td colspan='3'><b>Activation Procedures :</b></td></tr>
         <tr><td colspan='3' align='justify'> ".$act."</td></tr><br/>
         <tr><td colspan='3'><b>Sleep  :</b></td></tr>
         <tr><td colspan='3' align='justify'> ".$sleep."</td></tr></table></td></tr>
         </table></div>";
$headers = "From: $lemail";
$semi_rand = md5(time()); 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
$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" . $message . "\n\n"; 

foreach($_FILES as $userfile){
      // store the file information to variables for easier access
      $tmp_name = $userfile['tmp_name'];
      $type = $userfile['type'];
      $name = $userfile['name'];
      $size = $userfile['size'];

if (file_exists($tmp_name)){
         // check to make sure that it is an uploaded file and not a system file
        if(is_uploaded_file($tmp_name)){

            // open the file for a binary read
            $file = fopen($tmp_name,'rb');

            // read the file content into a variable
            $data = fread($file,filesize($tmp_name));

            // close the file
            fclose($file);

            // now we encode it and split it into acceptable length lines
            $data = chunk_split(base64_encode($data));
         }

  $message .= "--{$mime_boundary}\n" .
            "Content-Type: {$type};\n" .
            " name=\"{$name}\"\n" .
            "Content-Disposition: attachment;\n" .
            " filename=\"{$fileatt_name}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n";
}
}
// preparing attachments


// send

 $message .= "--{$mime_boundary}--\n";

$ok = @mail($to, $subject, $message, $headers);

上述代码在不附加文件的情​​况下发送邮件,并显示警告

Warning: file_exists() expects parameter 1 to be string, array given in ..my file

请帮我找到问题。我在另一个项目中使用了相同的代码并且运行良好,但唯一的区别是在该项目中我使用了4个单独的文件上传输入,但在这里我不能那样使用。

修改

我使用swiftmailer后出现了这些错误

Warning: basename() expects parameter 1 to be string, array given in C:\wamp\www\php\Teleneurology\swiftmailer-master\lib\classes\Swift\Mime\Attachment.php on line 139

Warning: strrpos() expects parameter 1 to be string, array given in C:\wamp\www\php\Teleneurology\swiftmailer-master\lib\classes\Swift\Mime\Attachment.php on line 143

var_dump($message)的ouptut是:

string(2945) "This is a multi-part message in MIME format. --==Multipart_Boundary_x65d975657f4969833a0b685447467ab3x Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit

Hi ,
A new report 
Details are:
Name     :  Email   :
Age        :    Date    : 21/10/2014
Gender  :   Mobile :
Center   :  History :
Address :





DIGITAL ELECTROENCEPHALOGRAPHY (DEEG) REPORT

Clinical Interpretation :
Select
Report :
Select
EKG :
Select
Activation Procedures :
Select
Sleep Record :
Select
--==Multipart_Boundary_x65d975657f4969833a0b685447467ab3x-- " 

1 个答案:

答案 0 :(得分:0)

$_FILES是多级数组。如果您回复print_r($_FILES);,您会看到问题。

尝试改变:

foreach($_FILES as $userfile){
    $tmp_name = $userfile['tmp_name'];
    $type = $userfile['type'];
    $name = $userfile['name'];
    $size = $userfile['size'];

为:

foreach ($_FILES['files']['name'] as $index => $name) {
    $tmp_name = $_FILES['files']['tmp_name'][$index];
    $type = $_FILES['files']['type'][$index];
    $size = $_FILES['files']['size'][$index];