发布日期:2018-03-26
怎样使用PHP Mail()发送附件+ 查看更多
怎样使用PHP Mail()发送附件
+ 查看更多
发布日期:2018-03-26 14:04
分类:PHP
浏览次数:335
我想要发送一个pdf文件作为邮件附件,能实现吗?
$to = "xxx"; $subject = "Subject" ; $message = 'Example message with html'; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: xxx' . "\r\n"; mail($to,$subject,$message,$headers);
是我遗漏了什么吗?
回答
我同意MihaiIorga的意见——使用PHPMailer脚本。听起来你好像拒绝使用它,因为你想要更简单的选择。相信我,相比用PHP内置的mail()函数来实现,PHPMailer简单得多。PHP的mail()函数实在是不太好。
要使用PHPMailer要完成以下步骤:
从这下载PHPMailer脚本:http://github.com/PHPMailer/PHPMailer
解压并将脚本的文件夹放到你工程中方便的位置。
把主脚本文件包含进来:
从这下载PHPMailer脚本:http://github.com/PHPMailer/PHPMailer
解压并将脚本的文件夹放到你工程中方便的位置。
把主脚本文件包含进来:
require_once('path/to/file/class.phpmailer.php');完成这些之后,发送邮件就由极其困难变得很简单了。
$email = new PHPMailer(); $email->From = 'you@example.com'; $email->FromName = 'Your Name'; $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();你没法要求更简单的方法了。
如果你用PHP的mail()函数来实现,你将会编写一大堆的代码,找bug也会变得很难。