通过PHP mail()函数发送简单的附件文件 [英] Sending a simple attached file via PHP mail() function

查看:312
本文介绍了通过PHP mail()函数发送简单的附件文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将再尝试一次,因为我的最后一个问题可能令人困惑. 我有一个简单网络表单,其中包含以下一些输入(现在,假设我有两个输入,即名称和文件输入).我希望用户上传文档(如果可能,则限制为.doc,.docx,.pdf,如果无法完成,则仅限制为.doc),并且我希望将大小限制为2MB以下.

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.

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

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.

任何帮助将不胜感激.

非常感谢, 阿米特(Amit)

Thanks a lot, Amit

推荐答案

可以使用列出的所有3个库(PHPMAiler,PEAR和Swiftmailer).

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

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

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('name@yourdomain.com', 'First Last');
  $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  $mail->SetFrom('name@yourdomain.com', 'First Last');
  $mail->AddReplyTo('name@yourdomain.com', '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将从您的服务器中获取一个文件.

AddAttachment will take a file from your server.

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

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.

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

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.

这篇关于通过PHP mail()函数发送简单的附件文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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