通过 PHP mail() 函数发送简单的附件

发布于 2024-09-08 19:05:03 字数 398 浏览 1 评论 0原文

我将再试一次,因为我的最后一个问题可能令人困惑。 我有一个简单网络表单,由以下一些输入组成(现在,假设我有两个输入,名称和文件输入)。我希望用户上传文档(如果可能的话限制为 .doc、.docx、.pdf,如果无法实现,我们就限制为 .doc),并且我想将大小限制在 2MB 以下。

让我重新表述一下。要附加的文件位于网络服务器上。它将动态上传到临时文件夹,通过邮件脚本发送,然后删除。

如果可以的话,拜托,我需要我能得到的所有帮助。

我尝试过 Swiftmailer、PHPMailer、PEAR,但似乎无法让它们工作。我所需要的只是一个简单的脚本来发送附件,仅此而已。无需验证,什么都没有。

任何帮助将不胜感激。

多谢, 阿米特

I'm going to give this another try because my last question might have been confusing.
I have a simple web form consisting of the following some inputs (for now, pretend i have two inputs, name and file input). I want the user to upload a document (if possible restrict to .doc, .docx, .pdf, if this is not possible to accomplish, let's just restrict to .doc), and i want to restrict the size to under 2MB.

Let me rephrase this. The file to be attached is NOT on the webserver. It will dynamically be uploaded to a temporary folder, send via the mail script, and then deleted.

If this is possible to accomplish, please, I need all the help that I can get.

I've tried Swiftmailer, PHPMailer, PEAR, I can't seem to get them to work. All I need is a simple script to send an attached file, nothing more. No validation necessary, nothing.

Any help would be greatly appreciated.

Thanks a lot,
Amit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

戏剧牡丹亭 2024-09-15 19:05:04

可以使用您列出的所有 3 个库(PHPMAiler、PEAR 和 Swiftmailer)。

对于 PHPMailer,您可以在此处查看教程

require_once '../class.phpmailer.php';

$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch

try {
  $mail->AddReplyTo('[email protected]', 'First Last');
  $mail->AddAddress('[email protected]', 'John Doe');
  $mail->SetFrom('[email protected]', 'First Last');
  $mail->AddReplyTo('[email protected]', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK<P></P>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

AddAttachment 将从您的服务器。

如何从 HTML 表单上传文件可以此处找到。电子邮件发送后,您可以从以下地址删除(取消链接)文件服务器。

PHP 手册可以帮助您更好地理解文件上传。

您想要做的所有事情都很容易实现,但解释起来比做起来要长:)但是通过我给您的所有链接,您已经拥有了所需的一切。如果您有具体问题,请告诉我。

It is possible to do with all 3 libraries you listed (PHPMAiler, PEAR and Swiftmailer).

For PHPMailer you can see a tutorial here:

require_once '../class.phpmailer.php';

$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch

try {
  $mail->AddReplyTo('[email protected]', 'First Last');
  $mail->AddAddress('[email protected]', 'John Doe');
  $mail->SetFrom('[email protected]', 'First Last');
  $mail->AddReplyTo('[email protected]', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK<P></P>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

AddAttachment will take a file from your server.

How to upload a file form an HTML form can be found here. Once your email is sent you can delete (unlink) the file from the server.

The PHP manual can help you to better undersand file uploads.

All you want to do is easy to achieve, but it's longer to explain than do it :) But with all the links I gave you you have everything you need. If you have specific questions let me know.

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