无法将附件发送到电子邮件

时间:2015-08-07 08:21:47

标签: php email

我正在尝试将附件发送到电子邮件。但我无法将附件文件与表单数据一起发送到电子邮件。我试过了

HTML:

        <form action="sendmail.php" method="post" name="form1" enctype="multipart/form-data">
    <table width="343" border="1">
        <tr>
        <td>Name</td>
        <td><input name="name" type="text" id="name"></td> 
    </tr> 
    <tr>
        <td>City</td>
        <td><input name="city" type="text" id="city"></td>
    </tr>
    <tr>
        <td>Descibe Yourself</td>
        <td><textarea name="description" cols="30" rows="4" id="description"></textarea></td>
    </tr>
    <tr>
        <td>Mobile Number</td>
        <td><input name="mobile" id="mobile"type="text"></td>
    </tr>
    <tr>
    <tr>
        <td>Email</td>
        <td><input name="email" type="email" id="email"></td>
    </tr>
    <tr>
      <td>Attach Your Image</td>
      <td><input name="fileAttach" type="file"></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td><input type="submit" name="Submit" value="Send"></td>
    </tr>
    </table>
    </form>

sendmail.php:

    <?
    $name = $_POST['name'];
    $city = $_POST['city'];
    $description = $_POST['description'];
    $mobile = $_POST['mobile'];
    $email = $_POST['email'];

    if ($name == '' || $city == '' || $description==''|| $mobile=='' || $email=='')
                    {
                echo "<script> alert('Fill all fields, try again!');
                window.location='contact.html'</script>";
                exit;
                }


    $ToEmail = 'info@mywebsite.com'; 
    $EmailSubject = 'Website contact form'; 
    $mailheader = "From: ".$_POST["email"]."\r\n"; 
    $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
    $MESSAGE_BODY = "Name: ".$_POST["name"]."<br><br>"; 
    $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
    $MESSAGE_BODY .= "Description: ".$_POST["description"]."<br>"; 
    $MESSAGE_BODY .= "Mobile: ".nl2br($_POST["mobile"])."<br>";
    $MESSAGE_BODY .= "City: ".nl2br($_POST["city"])."<br>";


    //*** Attachment ***//
    if($_FILES["fileAttach"]["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";
    }


     mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader, $strHeader) or die ("Failure");
    echo "<script> alert('Messgae successfully sent!');
    window.location='index.html'</script>";
       exit; 



    if($mail)
    {
        echo "<script> alert('Messgae successfully sent!');
    window.location='index.html'</script>";
    }
    else
    {
        echo "<script> alert('Temporary problem, try again!');</script>";
    }
?>

我在mail()中没有$ strHeader尝试相同的代码邮件被发送但是当我将$ strHeader添加到mail()时,我无法发送代码。请指导我在哪里做错了,并告诉我如何才能允许jpg,jpeg,png格式上传。我编辑的代码对我来说很容易学习。我在stackoverflow上检查了很多答案,但是我找不到答案。

2 个答案:

答案 0 :(得分:0)

尝试使用\ r \ n而不是\ n ...

$strHeader .= "--".$strSid."\r\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\r\n"; 
$strHeader .= "Content-Transfer-Encoding: base64\r\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\r\n\r\n";
$strHeader .= $strContent."\r\n\r\n";

修改

变化:

$mailheader = "From: ".$_POST["email"]."\r\n"; 
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br><br>"; 
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
$MESSAGE_BODY .= "Description: ".$_POST["description"]."<br>"; 
$MESSAGE_BODY .= "Mobile: ".nl2br($_POST["mobile"])."<br>";
$MESSAGE_BODY .= "City: ".nl2br($_POST["city"])."<br>";

使用:

$hash   = md5(date('r', time()));
$mailheader = "From: {$_POST["email"]}\r\n"; 
$mailheader .= "Reply-To: {$_POST["email"]}\r\n"; 
$mailheader .= "Content-type: text/html; boundary=\"PHP-mixed-$hash\""; 
$MESSAGE_BODY = "Name: {$_POST["name"]}<br><br>"; 
$MESSAGE_BODY .= "Email: {$_POST["email"]}<br>"; 
$MESSAGE_BODY .= "Description: {$_POST["description"]}<br>"; 
$MESSAGE_BODY .= "Mobile: ".nl2br($_POST["mobile"])."<br>";
$MESSAGE_BODY .= "City: ".nl2br($_POST["city"])."<br>";

并改变

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

使用

$strFilesName = $_FILES["fileAttach"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"]))); 
$MESSAGE_BODY .= "--PHP-mixed-$hash\r\n";
$MESSAGE_BODY .= "Content-Type: application/octet-stream; name=\"$strFilesName\"\r\n";
$MESSAGE_BODY .= "Content-Transfer-Encoding: base64\r\n";
$MESSAGE_BODY .= "Content-Disposition: attachment; filename=\"$strFilesName\"\r\n\r\n";
$MESSAGE_BODY .= $strContent."\r\n";
$MESSAGE_BODY .= "--PHP-mixed-$hash--";

你错过了边界。

答案 1 :(得分:0)

这已经回答了。 查看form not sending image file to email 以及我发送带附件的电子邮件的评论。