php中的文件附件邮件

时间:2013-01-19 07:28:30

标签: php html html5

你好我正在通过电子邮件处理html表单提交。服务器端脚本是P H P.我的html表单是

<form action='sendmail.php' method='post'>
   Name:<input type="text" name="name">
    File:<input type='file' name='attach'>
<input type="submit">

我发送电子邮件的php文件是

<?php 
   $strTo = "xyz@gmail.com";
     $strHeader .= "From: ".$_POST["name"]."<".$_POST["name"].">";  
     $strHeader .= "MIME-Version: 1.0\n";
     $strHeader .= "MIME-Version: 1.0\n"; 
   $strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";  
    $strHeader .= "This is a multi-part message in MIME format.\n";
   $strHeader .= "--".$strSid."\n"; 
    $strHeader .= "Content-type: text/html; charset=utf-8\n"; 
    $strHeader .= "Content-Transfer-Encoding: 7bit\n\n";  
    $strHeader .= $strMessage."\n\n";  
 if($_FILES["attach"]["name"] != "")
 {
        $strFilesName = $_FILES["fileAttach"]["name"];  
        $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));  
       $strHeader .= "--".$strSid."\n";  
         $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";  
       $strHeader .= "Content-Transfer-Encoding: base64\n";  
        $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";  
       $strHeader .= $strContent."\n\n";  
       }
 $flgSend = @mail($strTo,"no-subjet",null,$strHeader); 

但如果情况

则不会进入

1 个答案:

答案 0 :(得分:7)

尝试用以下内容替换您的表单:

<form action="sendmail.php" method="post" enctype="multipart/form-data">
    Name:<input type="text" name="name">
    File:<input type="file" name="attach" id="attach">
    <input type="submit">
</form>

表单中的 enctype 属性非常重要。

有关详细信息,请参阅以下链接: http://www.w3schools.com/php/php_file_upload.asp

同时确保PHP中的所有名称都与HTML匹配,例如文件输入的名称是 attach ,因此它在PHP中应该是相同的。

if($_FILES["attach"]["name"] != "")
{
    $strFilesName = $_FILES["attach"]["name"];
    ...
}