带附件的php邮件功能

时间:2014-10-24 06:55:04

标签: php email-attachments

我试图制作一封给我发邮件的表格。

这是html文件内容。

<form method="post" action="email.php" enctype="multipart/form-data">
<input type="text" id="contact_name" class="form-control" placeholder="Name" name="user_name"/>
<input type="text" id="contact_email" class="form-control" placeholder="Email Address"     
name="user_mail" <textarea id="contact_message" class="form-control" rows="7" placeholder="Write a 
message" name="user_message"></textarea>
<input name="attachment" type="file">      
<input type="submit" value="Send mail" class="btn btn-primary pull-right"/>
 </form>

这是php文件。

<?php

 if(isset($_POST) && !empty($_POST)) {
 if(!empty($_FILES['attachment']['name'])) {
   $file_name = $FILES['attachment']['name'];  
   $temp_name = $FILES['attachment']['tmp_name'];  
   $file_type = $FILES['attachment']['type'];

   $base = basename($file_name);
   $extension = substr($base, strlen($base)-4, strlen($base));

   //only these file types will be allowed
   $allowed_extensions = array(".doc", "docx", ".pdf", ".zip", ".png");

   //check that this file type is allowed
   if(in_array($extension,$allowed_extensions)) {
       //mail essentials
       $from = $_POST['user_mail'];
       $to = "eugenepi1025@gmail.com";
       $subject = $_POST['user_name'];
       $message = $_POST['user_message'];

       //things u need
       $file = $temp_name;
       $content = chunk_split(base64_encode(file_get_contents($file)));
       $uid = md5(uniqid(time()));  //unique identifier

       //standard mail headers
       $header = "From: ".$from."\r\n";
       $header .= "Reply-To: ".$replyto. "\r\n";
       $header .= "MIME-Version: 1.0\r\n";


       //declare multiple kinds of email (plain text + attch)
       $header .="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
       $header .="This is a multi-part message in MIME format.\r\n";

       //plain txt part

       $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";


       //attch part
       $header .= "--".$uid."\r\n";
       $header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
       $header .= "Content-Transfer-Encoding: base64\r\n";
       $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
       $header .= $content."\r\n\r\n";  //chucked up 64 encoded attch


       //sending the mail - message is not here, but in the header in a multi part

       if(mail($to, $subject, "", $header)) {
       echo "success";
       }else {
           echo "fail";
       }


   }else {
       echo "file type not allowed"; }    //echo an html file
}else {
  echo "no file posted"; }    
}
?>

事情是file type not allowed消息不断传来。但是我上传了一个.png文件,所以我不知道是什么问题。

你可以帮我解决这个问题吗?我已经坚持了几个小时:(

3 个答案:

答案 0 :(得分:5)

实际上你有

$file_name = $FILES['attachment']['name'];  
$temp_name = $FILES['attachment']['tmp_name'];  
$file_type = $FILES['attachment']['type'];

应该是

$file_name = $_FILES['attachment']['name'];  
$temp_name = $_FILES['attachment']['tmp_name'];  
$file_type = $_FILES['attachment']['type'];

它完美无缺:))

答案 1 :(得分:3)

另外,请确保为您的图片附件标题Content-Type分配了正确的MIME类型,即image/png .png文件。请参阅此wiki以获取图像MIME类型列表。

答案 2 :(得分:1)

您已超过substr()中的字符串大小,因此$extension无法正确显示。

a.pdf; strlen($base)是5;所以结束索引是4.因此,你的第三个参数应该是strlen($base) - 1而不是strlen($base)

按如下方式更改行:

$extension = substr($base, strlen($base) - 4, strlen($base) - 1);

我认为这种方法更好:

$extension = substr($base, strrpos($base, ".")-1);

哪个会给你最后一个.字符和字符串的其余部分,a.k.a。文件扩展名。

Demo