使用PHP Mail()发送附件? [英] Send attachments with PHP Mail()?

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

问题描述

我需要通过邮件发送pdf文件,可以吗?

I need to send a pdf with mail, is it possible?

$to = "xxx";
$subject = "Subject" ;
$message = 'Example message with <b>html</b>';
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: xxx <xxx>' . "\r\n";
mail($to,$subject,$message,$headers);

我想念什么?

推荐答案

我在评论中同意@MihaiIorga –使用PHPMailer脚本.您听起来好像要拒绝它,因为您想要更简单的选择.相信我,与尝试使用PHP的内置mail()函数自己进行操作相比,PHPMailer 是一个非常容易的选择. PHP的mail()函数确实不是很好.

I agree with @MihaiIorga in the comments – use the PHPMailer script. You sound like you're rejecting it because you want the easier option. Trust me, PHPMailer is the easier option by a very large margin compared to trying to do it yourself with PHP's built-in mail() function. PHP's mail() function really isn't very good.

要使用PHPMailer:

To use PHPMailer:

  • 从此处下载PHPMailer脚本: http://github.com/PHPMailer/PHPMailer
  • 提取档案并将脚本的文件夹复制到项目中的方便位置.
  • 包括主脚本文件-require_once('path/to/file/class.phpmailer.php');
  • Download the PHPMailer script from here: http://github.com/PHPMailer/PHPMailer
  • Extract the archive and copy the script's folder to a convenient place in your project.
  • Include the main script file -- require_once('path/to/file/class.phpmailer.php');

现在,发送带有附件的电子邮件已从疯狂的困难变成了难以置信的轻松:

Now, sending emails with attachments goes from being insanely difficult to incredibly easy:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$email = new PHPMailer();
$email->SetFrom('you@example.com', 'Your Name'); //Name is optional
$email->Subject   = 'Message Subject';
$email->Body      = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';

$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );

return $email->Send();

只有一行$email->AddAttachment();-您再也想不到了.

It's just that one line $email->AddAttachment(); -- you couldn't ask for any easier.

如果使用PHP的mail()函数执行此操作,则将编写代码堆栈,并且可能会发现很多非常难以发现的错误.

If you do it with PHP's mail() function, you'll be writing stacks of code, and you'll probably have lots of really difficult to find bugs.

这篇关于使用PHP Mail()发送附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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