2

我有一个表格,可让您输入数据并附加文件并通过电子邮件发送。它适用于我的手机、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.';
    }
  }    

?>
4

2 回答 2

2

IE 没有执行表单,因为使用<button>withouttype="submit"作为提交的一种方式,例如您目前在表单中的那个:

<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 内容处置:附件;文件名="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 处理程序 (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.";
}
?>
于 2013-09-18T03:35:19.747 回答
0

查看此 Stack Overflow 问题的答案: PHP 表单在 Internet Explorer 中不起作用

它可能是表单结构中的东西。

于 2013-09-18T02:24:47.357 回答