简单的 PHP 表单:电子邮件附件(代码 Golf)
想象一下,一个用户想要在他们的网站上放置一个表单,该表单将允许网站访问者上传一个文件和一条简单的消息,该消息将立即通过电子邮件发送(即,该文件未存储在服务器上,或者如果该文件存储在服务器上)仅暂时)作为文件附件,并在邮件正文中添加注释。
请访问 http://a2zsollution.com/php-secure-e-mail/
实现此目的最简单的方法是什么?
最简单的方面:
- 大小(代码高尔夫)
- 易于实现(理想情况下全部在一个文件中,需要很少甚至不需要外部资源)
- 不会为了混淆而混淆(大小的权衡很好)
- 自包含示例(如果在没有表单的情况下调用 )发布,它显示表单)
这几乎是相反的: 如何从 PHP 获取电子邮件及其附件。 它几乎可以在 用 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.
See more details at http://a2zsollution.com/php-secure-e-mail/
What is the simplest way to accomplish this?
Simplest in terms of:
- Size (code golf)
- Ease of implementation (ideally all in one file, needs few to no external resources)
- Not obfuscated for the sake of obfuscation (tradeoffs for size are fine)
- Self contained example (if called without a form post, it displays the form)
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为了将文件作为附件添加到电子邮件中,需要将其短暂存储在服务器上。 不过,将其放置在临时位置,然后在完成后将其删除,这很简单。
至于电子邮件,Zend Mail 有一个非常易于使用的界面处理电子邮件附件。 我们运行时安装了整个 Zend Framework,但我很确定您可以只安装 Zend_Mail 库,而不需要任何其他模块作为依赖项。
使用 Zend_Mail,发送一封带有附件的电子邮件就像这样简单:
如果您正在寻找一个单文件包来完成整个表单/电子邮件/附件的工作,我还没有看到。 但各个组件肯定是可用的并且易于组装。 最棘手的事情是电子邮件附件,上面的建议使它变得非常简单。
In order to add the file to the email as an attachment, it will need to be stored on the server briefly. It's trivial, though, to place it in a tmp location then delete it after you're done with it.
As for emailing, Zend Mail has a very easy to use interface for dealing with email attachments. We run with the whole Zend Framework installed, but I'm pretty sure you could just install the Zend_Mail library without needing any other modules for dependencies.
With Zend_Mail, sending an email with an attachment is as simple as:
If you're looking for a one-file-package to do the whole form/email/attachment thing, I haven't seen one. But the individual components are certainly available and easy to assemble. Trickiest thing of the whole bunch is the email attachment, which the above recommendation makes very simple.
我还没有测试其中的电子邮件部分(我的测试盒不发送电子邮件),但我认为它会起作用。
I haven't tested the email part of this (my test box does not send email) but I think it will work.
PEAR::Mail_Mime? 当然,(最少)2 个文件的 PEAR 依赖项(如果您编辑它以删除 pear 依赖项,则只是 mail_mime 本身),但它运行良好。 此外,大多数服务器都在某种程度上安装了 PEAR,并且在最好的情况下,它们安装了 Pear/Mail 和 Pear/Mail_Mime。 对于提供相同功能的大多数其他库来说,这是不能说的。
您还可以考虑查看 PHP 的 IMAP 扩展。 它有点复杂,需要更多设置(默认情况下未启用或安装),但在编译消息并将消息发送到支持 IMAP 的服务器方面必须更有效。
PEAR::Mail_Mime? Sure, PEAR dependency of (min) 2 files (just mail_mime itself if you edit it to remove the pear dependencies), but it works well. Additionally, most servers have PEAR installed to some extent, and in the best cases they have Pear/Mail and Pear/Mail_Mime. Something that cannot be said for most other libraries offering the same functionality.
You may also consider looking in to PHP's IMAP extension. It's a little more complicated, and requires more setup (not enabled or installed by default), but is must more efficient at compilng and sending messages to an IMAP capable server.
只是为了好玩,我想我会把它敲起来。 它最终比我想象的更棘手,因为我没有完全理解边界部分是如何工作的,最终我发现开头和结尾的“--”很重要,然后就结束了。
确实非常准系统,显然使用内联 CSS 来隐藏表单有点便宜,而且您几乎肯定希望向用户提供更多反馈! 另外,我可能会花更多的时间来确定文件的实际内容类型是什么,而不是作弊和使用应用程序/八位字节流,但这部分也很有趣。
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.
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.
此 http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment 的组合
使用 php 上传文件示例就可以了。 在上传文件示例中,您无需使用 move_uploaded_file 将其从临时文件夹中移动,只需打开它:
where
$tmp_file = $_FILES['userfile']['tmp_name'];
并将其发送为像示例的其余部分一样的附件。
所有内容都在一个文件中/自包含:
我认为这是一个快速练习,可以根据上述两个可用示例获得您需要的工作。
PS 在 Apache 将其传递给 PHP 来执行它想要的操作之前,它需要上传到某个地方。 默认情况下,这将是系统的临时文件夹,除非在配置文件中进行了更改。
A combination of this http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment
with the php upload file example would work. In the upload file example instead of using move_uploaded_file to move it from the temporary folder you would just open it:
where
$tmp_file = $_FILES['userfile']['tmp_name'];
and send it as an attachment like the rest of the example.
All in one file / self contained:
I think its a quick exercise to get what you need working based on the above two available examples.
P.S. It needs to get uploaded somewhere before Apache passes it along to PHP to do what it wants with it. That would be your system's temp folder by default unless it was changed in the config file.
本文“如何创建基于 PHP 的电子邮件表单带文件附件”提供了如何实现您的要求的分步说明。
引用:
它由以下步骤组成:
脚本
上传的文件
可以下载整个示例代码 此处
This article "How to create PHP based email form with file attachment" presents step-by-step instructions how to achieve your requirement.
Quote:
It consists of the following steps:
script
the uploaded file
The entire example code can be downloaded here