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

查看:46
本文介绍了使用 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' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
$headers .= 'From: xxx <xxx>' . "
";
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');

现在,发送带有附件的电子邮件从非常困难变得非常容易:

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

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;

$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天全站免登陆