简单的 PHP 表单:电子邮件附件(代码 Golf)

发布于 2024-07-19 16:21:35 字数 744 浏览 6 评论 0原文

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

请访问 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

青朷 2024-07-26 16:21:35

为了将文件作为附件添加到电子邮件中,需要将其短暂存储在服务器上。 不过,将其放置在临时位置,然后在完成后将其删除,这很简单。

至于电子邮件,Zend Mail 有一个非常易于使用的界面处理电子邮件附件。 我们运行时安装了整个 Zend Framework,但我很确定您可以只安装 Zend_Mail 库,而不需要任何其他模块作为依赖项。

使用 Zend_Mail,发送一封带有附件的电子邮件就像这样简单:

$mail = new Zend_Mail();
$mail->setSubject("My Email with Attachment");
$mail->addTo("[email protected]");
$mail->setBodyText("Look at the attachment");
$attachment = $mail->createAttachment(file_get_contents('/path/to/file'));
$mail->send();

如果您正在寻找一个单文件包来完成整个表单/电子邮件/附件的工作,我还没有看到。 但各个组件肯定是可用的并且易于组装。 最棘手的事情是电子邮件附件,上面的建议使它变得非常简单。

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:

$mail = new Zend_Mail();
$mail->setSubject("My Email with Attachment");
$mail->addTo("[email protected]");
$mail->setBodyText("Look at the attachment");
$attachment = $mail->createAttachment(file_get_contents('/path/to/file'));
$mail->send();

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.

_畞蕅 2024-07-26 16:21:35

我还没有测试其中的电子邮件部分(我的测试盒不发送电子邮件),但我认为它会起作用。

<?php
if ($_POST) {
$s = md5(rand());
mail('[email protected]', 'attachment', "--$s

{$_POST['m']}
--$s
Content-Type: application/octet-stream; name=\"f\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

".chunk_split(base64_encode(join(file($_FILES['f']['tmp_name']))))."
--$s--", "MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"$s\"");
exit;
}
?>
<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<textarea name="m"></textarea><br>
<input type="file" name="f"/><br>
<input type="submit">
</form>

I haven't tested the email part of this (my test box does not send email) but I think it will work.

<?php
if ($_POST) {
$s = md5(rand());
mail('[email protected]', 'attachment', "--$s

{$_POST['m']}
--$s
Content-Type: application/octet-stream; name=\"f\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

".chunk_split(base64_encode(join(file($_FILES['f']['tmp_name']))))."
--$s--", "MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"$s\"");
exit;
}
?>
<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<textarea name="m"></textarea><br>
<input type="file" name="f"/><br>
<input type="submit">
</form>
当梦初醒 2024-07-26 16:21:35

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.

剩余の解释 2024-07-26 16:21:35

只是为了好玩,我想我会把它敲起来。 它最终比我想象的更棘手,因为我没有完全理解边界部分是如何工作的,最终我发现开头和结尾的“--”很重要,然后就结束了。

<?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]';
        $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]\r\nReply-To: [email protected]";
        $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 来隐藏表单有点便宜,而且您几乎肯定希望向用户提供更多反馈! 另外,我可能会花更多的时间来确定文件的实际内容类型是什么,而不是作弊和使用应用程序/八位字节流,但这部分也很有趣。

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 = '[email protected]';
        $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]\r\nReply-To: [email protected]";
        $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>

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.

深海夜未眠 2024-07-26 16:21:35

http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment 的组合

使用 php 上传文件示例就可以了。 在上传文件示例中,您无需使用 move_uploaded_file 将其从临时文件夹中移动,只需打开它:

$attachment = chunk_split(base64_encode(file_get_contents($tmp_file))); 

where $tmp_file = $_FILES['userfile']['tmp_name'];

并将其发送为像示例的其余部分一样的附件。

所有内容都在一个文件中/自包含:

<? if(isset($_POST['submit'])){
//process and email
}else{
//display form
}
?>

我认为这是一个快速练习,可以根据上述两个可用示例获得您需要的工作。

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:

$attachment = chunk_split(base64_encode(file_get_contents($tmp_file))); 

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:

<? if(isset($_POST['submit'])){
//process and email
}else{
//display form
}
?>

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.

放血 2024-07-26 16:21:35

本文“如何创建基于 PHP 的电子邮件表单带文件附件”提供了如何实现您的要求的分步说明。

引用:

本文向您展示如何创建
基于 PHP 的电子邮件表单,支持
文件附件。 文章还将
向您展示如何验证类型和
上传文件的大小。

它由以下步骤组成:

  • 带有文件上传框的 HTML 表单
  • 在 PHP 中获取上传的文件
    脚本
  • 验证大小和扩展名
    上传的文件
  • 复制上传的文件
  • 发送电子邮件

可以下载整个示例代码 此处

This article "How to create PHP based email form with file attachment" presents step-by-step instructions how to achieve your requirement.

Quote:

This article shows you how to create a
PHP based email form that supports
file attachment. The article will also
show you how to validate the type and
size of the uploaded file.

It consists of the following steps:

  • The HTML form with file upload box
  • Getting the uploaded file in the PHP
    script
  • Validating the size and extension of
    the uploaded file
  • Copy the uploaded file
  • Sending the Email

The entire example code can be downloaded here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文