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