简单的 PHP 表单:电子邮件附件(代码高尔夫) [英] Simple PHP form: Attachment to email (code golf)

查看:26
本文介绍了简单的 PHP 表单:电子邮件附件(代码高尔夫)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,一个用户想要在他们的网站上放置一个表单,允许网站访问者上传一个文件和一条简单的消息,该消息将立即通过电子邮件发送(即,该文件未存储在服务器上,或者如果它只是暂时的)作为文件附件,在邮件正文中带有注释.

Imagine a user that would like to put a form on their website that would allow a website visitor to upload a file and a simple message, which will immediately be emailed (ie, the file is not stored on the server, or if it is then only temporarily) as a file attachment with the note in the message body.

http://a2zsollution.com/php-secure-e-mail 上查看更多详细信息/

实现此目的最简单的方法是什么?

What is the simplest way to accomplish this?

最简单的:

  • 尺寸(代码高尔夫)
  • 易于实施(理想情况下所有内容都在一个文件中,几乎不需要外部资源)
  • 不会为了混淆而混淆(大小权衡很好)
  • 自包含示例(如果在没有表单帖子的情况下调用,则显示表单)

这几乎是相反的:如何获取电子邮件以及来自 PHP 的附件.它几乎可以在 在 PHP 中编译带有多个附件的电子邮件 中得到解答,但它实际上并不显示代码.

This is nearly the reverse of: How to get email and their attachments from PHP. It almost could have been answered in Compiling email with multiple attachments in PHP, but it doesn't actually show code.

推荐答案

只是为了好玩,我想我会敲掉它.结果比我想象的更棘手,因为我没有完全理解边界部分是如何工作的,最终我发现开始和结束的--"很重要,而且没有了.

Just for fun I thought I'd knock it up. It ended up being trickier than I thought because I went in not fully understanding how the boundary part works, eventually I worked out that the starting and ending '--' were significant and off it went.

<?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 = 'me@example.com';
        $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: webmaster@example.com
Reply-To: webmaster@example.com";
        $headers .= "
MIME-Version: 1.0
Content-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 来隐藏表单有点便宜,而且您几乎肯定希望向用户提供更多反馈!此外,我可能会花更多的时间来确定文件的实际内容类型是什么,而不是欺骗和使用 application/octet-stream,但这部分同样有趣.

Very barebones really, and obviously the using inline CSS to hide the form is a bit cheap and you'd almost certainly want a bit more feedback to the user! Also, I'd probably spend a bit more time working out what the actual Content-Type for the file is, rather than cheating and using application/octet-stream but that part is quite as interesting.

这篇关于简单的 PHP 表单:电子邮件附件(代码高尔夫)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆