想象一下,一个用户想要在其网站上放置一个表单,该表单将允许网站访问者上传一个文件和一条简单的消息,该消息将立即通过电子邮件发送(即,该文件未存储在服务器上,或者如果该文件存储在服务器上)仅暂时)作为文件附件,并在邮件正文中添加注释。
查看更多详细信息http://a2zsollution.com/php-secure-e-mail/ http://a2zsollution.com/php-secure-e-mail/
实现这一目标的最简单方法是什么?
最简单的方面:
- 尺寸(代码高尔夫)
- 易于实施(理想情况下全部在一个文件中,需要很少甚至不需要外部资源)
- 不为了混淆而混淆(大小的权衡是可以的)
- 自包含示例(如果在没有表单发布的情况下调用,它将显示表单)
这几乎是相反的:如何从 PHP 获取电子邮件及其附件 https://stackoverflow.com/questions/114953/how-to-get-email-and-their-attachments-from-php。这几乎可以在用 PHP 编译带有多个附件的电子邮件 https://stackoverflow.com/questions/720455/compiling-email-with-multiple-attachments-in-php,但它实际上并不显示代码。
只是为了好玩,我想我会把它敲掉。它最终比我想象的更棘手,因为我没有完全理解边界部分是如何工作的,最终我发现开头和结尾的“--”很重要,然后就结束了。
<?php
if(isset($_POST['submit']))
{
//The form has been submitted, prep a nice thank you message
$output = '<h1>Thanks for your file and message!</h1>';
//Set the form flag to no display (cheap way!)
$flags = 'style="display:none;"';
//Deal with the email
$to = '[email protected] /cdn-cgi/l/email-protection';
$subject = 'a file for you';
$message = strip_tags($_POST['message']);
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
$filename = $_FILES['file']['name'];
$boundary =md5(date('r', time()));
$headers = "From: [email protected] /cdn-cgi/l/email-protection\r\nReply-To: [email protected] /cdn-cgi/l/email-protection";
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$message="This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
$message
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
mail($to, $subject, $message, $headers);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>MailFile</title>
</head>
<body>
<?php echo $output; ?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
<p><label for="message">Message</label> <textarea name="message" id="message" cols="20" rows="8"></textarea></p>
<p><label for="file">File</label> <input type="file" name="file" id="file"></p>
<p><input type="submit" name="submit" id="submit" value="send"></p>
</form>
</body>
</html>
确实非常准系统,显然使用内联 CSS 来隐藏表单有点便宜,而且您几乎肯定希望向用户提供更多反馈!另外,我可能会花更多的时间来确定文件的实际内容类型是什么,而不是作弊和使用应用程序/八位字节流,但这部分也很有趣。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)