使用PHP无法在IE 8中使用电子邮件发送文件

时间:2013-09-18 02:19:43

标签: php html5 forms internet-explorer

我有一个表单,允许您输入数据并附加文件并通过电子邮件发送。它适用于我的手机,Safari,Firefox和Chrome,但我不能让它在IE中工作。 (它甚至不会发布到php页面。)

HTML:

<form method="post" id="mainForm" enctype="multipart/form-data" action="email.php">
        <span class="hide" id="status"></span>
        <div class="rowElem">
            <label for="name">Member Name:</label>
            <div class="formRight">
                <input id="name" type="text" name="name" required placeholder="John Doe" />
            </div>
        </div>
        <div class="rowElem">
            <label for="email">Member Email: </label>
            <div class="formRight">
                <input id="email" type="email" name="email"  required placeholder="john.doe@email.com" />
            </div>
        </div>
        <div class="rowElem">
            <label for="subject">Subject: </label>
            <div class="formRight">
                <input id="subject" type="text" name="subject"  required placeholder="Phone Call Follow Up"/>
            </div>
        </div>
        <div class="rowElem">
            <label for="message">Message: </label>
            <div class="formRight">
                <textarea id="message" name="message"  required ></textarea> 
            </div>
        </div>
        <div class="rowElem">
            <label for="attachment">Attachment: </label>
            <div class="formRight">
                <input id="attachment" type="file" name="attachment"/>
            </div>
        </div>
        <div class="rowElem">
            <button class="submit">Send Email</button>
            <button class="reset" onClick="resetForm()">Reset Form</button>
        </div>
    </form>

PHP:

<?php 
    $membername = $_POST['name'];
    $email = $_POST['email'];
    $content = $_POST['message'];
    $subject = $_POST['subject'];

    $maxTotalAttachments=5097152; //Maximum of 2 MB total attachments, in bytes
    $boundary_text = uniqid();
    $boundary = "--".$boundary_text."\r\n";
    $boundary_last = "--".$boundary_text."--\r\n";

     //Build up the list of attachments, 
    //getting a total size and adding boundaries as needed
     $emailAttachments = "";
     $totalAttachmentSize = 0;
     foreach ($_FILES as $file) {
        //In case some file inputs are left blank - ignore them
        if ($file['error'] == 0 && $file['size'] > 0){
             $fileContents = file_get_contents($file['tmp_name']);
             $totalAttachmentSize += $file['size']; //size in bytes
             $emailAttachments .= "Content-Type: " 
            .$file['type'] . "; name=\"" . basename($file['name']) . "\"\r\n"
            ."Content-Transfer-Encoding: base64\r\n"
            ."Content-disposition: attachment; filename=\"" 
            .basename($file['name']) . "\"\r\n"
            ."\r\n"
            //Convert the file's binary info into ASCII characters
            .chunk_split(base64_encode($fileContents))
             .$boundary;
         }
    }
  //Now all the attachment data is ready to insert into the email body.
  //If the file was too big for PHP, it may show as having 0 size
  if ($totalAttachmentSize == 0) {
    echo "Message not sent. Either no file was attached, or it was bigger than PHP is configured to accept.  ".basename($file['name']);
}
  //Now make sure it doesn't exceed this function's specified limit:
  else if ($totalAttachmentSize>$maxTotalAttachments) {
    echo "Message not sent. Total attachments can't exceed " .  $maxTotalAttachments . " bytes.";
  }
  //Everything is OK - let's build up the email
  else {  
    $to = $email;
    $subject = $subject;
    $from = "do-not-reply@someemail.com";
    $headers = "From: ".$from." \r\n";
    $headers .= 'Bcc: someone@someemail.com' . "\r\n";
    $headers .=     "MIME-Version: 1.0\r\n"
    ."Content-Type: multipart/mixed; boundary=\"$boundary_text\"" . "\r\n";  
    $message .="If you can see this, your email client "
    ."doesn't accept MIME types!\r\n"
    .$boundary;

    $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
    ."Content-Transfer-Encoding: 7bit\r\n\r\n" 
    //Inert the HTML message body you passed into this function
    ."Hello ".$membername."<br><br>" . $content . "<br><br> Please do not reply to this message. Replies to this message are routed to an unmonitored mailbox. If you have questions please go to... You may also call us at .... Thank you. " . "\r\n";

    //Insert the attachment information we built up above.
    //Each of those attachments ends in a regular boundary string   

    $message .= $emailAttachments

    //This section ends in a terminating boundary string - meaning
    //"that was the last section, we're done"
    .$boundary_last;

    if(mail($to,$subject,$message,$headers)){
     echo 'some html goes here.';  
    } 
    else {
      echo 'Error - mail not sent.';
    }
  }    

?>

2 个答案:

答案 0 :(得分:2)

由于使用<button>而不使用type="submit"作为提交方式,例如您目前在表单中提供的方式,IE未执行该表单:

<button class="submit">Send Email</button> 

您可以使用如下输入类型:

<input type="submit" name="submit" value="Send Email">

或将type="submit"添加到您当前的按钮,例如:

<button class="submit" type="submit">Send Email</button>

我测试了它和IE,它不希望将表单作为“按钮”发送而不包含type="submit"

另一个问题是处理程序。标题信息被泄露到邮件正文区域本身,所以我在下面的答案中使用了一个附件处理程序。

例如,以下是泄漏邮件正文的部分副本:

  

Hello Fred

     

测试消息

     

请不要回复此邮件。对此邮件的回复将路由到不受监控的邮箱。如果您有任何疑问,请转到......您也可以致电我们....谢谢。内容类型:image / jpeg; name =“test_image.jpg”Content-Transfer-Encoding:base64 Content-disposition:attachment; filename =“test_image.jpg”LzlqLzRBQVFTa1pKUmdBQkFnQUFaQUJrQUFELzdBQVJSSFZqYTNrQUFRQUVB .............................

使用以下表单和处理程序,能够成功发送带有IE 7和FF 24附件的电子邮件。

HTML表单

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="sendmail_attach.php" method="post" name="form1" enctype="multipart/form-data">
<table width="343" border="1">

<tr>
<td>Name</td>
<td><input name="txtFormName" type="text"></td>
</tr>
<tr>
<tr>
<td>Form Email</td>
<td><input name="txtFormEmail" type="text"></td>
</tr>

<tr>
<td>Message:</td>
<td><textarea name="txtDescription" cols="30" rows="4" id="txtDescription"></textarea></td>
</tr>

<tr>
<td>Attachment</td>
<td><input name="fileAttach" type="file"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="Send Message"></td>
</tr>
</table>
</form>
</body>
</html>

PHP hander(sendmail_attach.php)

<?php
$strTo = "email@example.com";
$strSubject = "Email attach subject";
$strMessage = nl2br($_POST["txtDescription"]);

//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));

$strHeader = "";
$strHeader .= "From: ".$_POST["txtFormName"]."<".$_POST["txtFormEmail"].">\nReply-To: ".$_POST["txtFormEmail"]."";

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

//*** 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";
}

$flgSend = @mail($strTo,$strSubject,null,$strHeader);  // @ = No Show Error //

if($flgSend)
{
echo "Mail send completed.";
}
else
{
echo "Cannot send mail.";
}
?>

答案 1 :(得分:0)

查看此Stack Overflow问题的答案: PHP form not working in Internet Explorer

它可能是表单结构中的内容。