使用 PHP 通过电子邮件发送文件在 IE 8 中不起作用


Emailing file using PHP not working in IE 8

>我有一个表格,允许您输入数据并附加文件并通过电子邮件发送。它适用于我的手机、野生动物园、火狐和 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.';
    }
  }    
?>

IE没有执行表单,因为使用了没有type="submit" <button>作为提交方式,例如您目前表单中的表单:

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

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

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

你好弗雷德

测试消息

请不要回复此消息。对此邮件的答复将路由到不受监视的邮箱。如果您有任何疑问,请访问...您也可以致电我们....谢谢。内容类型:图像/jpeg;name="test_image.jpg" 内容传输编码:base64 内容处置:附件;filename="test_image.jpg" LzlqLzRBQVFTa1pKUmdBQkFnQUFaQUJrQUFELzdBQVJSSFZqYTNrQUFRQUVB...................................

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

网页表单

<!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.";
}
?>

查看这个堆栈溢出问题的答案:PHP 表单在 Internet Explorer 中不起作用

它可能是形式结构中的东西。