使用php的电子邮件附件,无法在电子邮件中打开文件

时间:2018-06-28 06:13:17

标签: php html email

这是我的代码,在这种情况下,代码可以正常工作,并且邮件中包含附件,但是附件文件无法打开。然后我下载了文件并尝试将其打开,它显示错误“不是受支持的文件类型或文件已损坏(例如,它以电子邮件附件的形式发送并且未正确解码)”

<form action="#" method="POST" enctype="multipart/form-data"  >
 <input type="file" name="csv_file[]" />
 <br/>

<input type="file" name="csv_file[]" />
 <br/>

  <input type="file" name="csv_file[]" />
 <br/>

 <input type="submit" name="upload" value="Upload" />
 <br/>

 </form> 

 <?php

 if($_POST) {

  for($i=0; $i < count($_FILES['csv_file']['name']); $i++){

    $ftype[]       = $_FILES['csv_file']['type'][$i];
    $fname[]       = $_FILES['csv_file']['name'][$i];

}


// array with filenames to be sent as attachment
$files = $fname;

// email fields: to, from, subject, and so on
$to = "example@gmail.com";
$from = "example@gmail.com"; 
$subject ="My subject"; 
$message = "My message1";
$headers = "From: $from";

// 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 
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$message .= "--{$mime_boundary}\n";

// preparing attachments
for($x=0;$x<count($files);$x++){
    $file = fopen($files[$x],"rb");
    $data = fread($file,filesize($files[$x]));
    fclose($file);
    $data = chunk_split(base64_encode($data));
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
    "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
    "Content-Transfer-Encoding: base64\n\n $data \n\n";
    $message .= "--{$mime_boundary}\n";
}

// send

$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
    echo "<p>mail sent to $to!</p>"; 
} else { 
    echo "<p>mail could not be sent!</p>"; 
} 



 }

 ?>

1 个答案:

答案 0 :(得分:0)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 <title>E-mail with Attachment</title>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 </head>
 <body>
 <?php
 if ($_SERVER['REQUEST_METHOD']=="POST"){

  // we'll begin by assigning the To address and message subject
   $to="example@gmail.com";
   $subject="E-mail with attachment";

  // get the sender's name and email address
  // we'll just plug them a variable to be used later
  $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";

  // generate a random string to be used as the boundary marker
  $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

   // now we'll build the message headers
   $headers = "From: $from\r\n" .
   "MIME-Version: 1.0\r\n" .
   "Content-Type: multipart/mixed;\r\n" .
  " boundary=\"{$mime_boundary}\"";
    $message="This is an example";
    $message .= "Name:".$_POST["fromname"]."Message Posted:".$_POST["modlist"];
    $message = "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
     $message . "\n\n";

   // now we'll process our uploaded files
   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 the upload succeded, the file will exist
  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));
     }

     // now we'll insert a boundary to indicate we're starting the attachment
     // we have to specify the content type, file name, and disposition as
     // an attachment, then add the file content.
     // NOTE: we don't set another boundary to indicate that the end of the 
     // file has been reached here. we only want one boundary between each file
     // we'll add the final one after the loop finishes.
     $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";
  }
 }
  // here's our closing mime boundary that indicates the last of the message
  $message.="--{$mime_boundary}--\n";
  // now we just send the message
  if (@mail($to, $subject, $message, $headers))
  echo "Message Sent";
  else
  echo "Failed to send";
 } else {
 ?>
 <p>Send an e-mail with an attachment:</p>
 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1">
  <p>Your name: <input type="text" name="fromname"></p>
   <p>Your e-mail: <input type="text" name="fromemail"></p>
   <p>Mod List: <textarea  name="question" maxlength="1000" cols="25" rows="6" name="modlist"></textarea>
  <p>File: <input type="file" name="file1"></p>
  <p>File: <input type="file" name="file2"></p>
  <p>File: <input type="file" name="file3"></p>
  <p>File: <input type="file" name="file4"></p>
  <p>File: <input type="file" name="file5"></p>
  <p>File: <input type="file" name="file6"></p>
  <p>File: <input type="file" name="file7"></p>
  <p><input type="submit" name="Submit" value="Submit"></p>
  </form>
  <?php } ?>
  </body>
  </html>