一、swift mailer简介
swift mailer是一个php邮件发送类。它不依赖于php自带的mail()函数,因为该函数在发送多个邮件时占用的系统资源很高。swift直接与smtp 服务器通讯,具有非常高的发送速度和效率。
swift mailer的特点:
1、使用smtp、sendmail、postfix或者你自己可以自定义传输实现发送电子邮件
2、需要用户名和密码和或加密支持的服务器
3、从头部注入攻击保护不请求数据的内容
4、发送mime兼容的html/multipart邮件
5、使用事件驱动的插件方式来定制库
6、处理大附件和内嵌/嵌入图像时低内存占用
7、swiftmailer发邮件效率比phpmailer高很多,使用起来很方便
二、swift mailer使用
require_once 'lib/swift_required.php';function sendmail(){/*$transport = swift_smtptransport::newinstance('smtp.163.com', 25);$transport->setusername('username@163.com');$transport->setpassword('password');$transport = swift_sendmailtransport::newinstance('/usr/sbin/exim -bs');$transport = swift_mailtransport::newinstance();*/$transport = swift_smtptransport::newinstance('smtp.163.com', 25);$transport->setusername('username@163.com');$transport->setpassword('password');$mailer = swift_mailer::newinstance($transport);$message = swift_message::newinstance();$message->setfrom(array('username@163.com' => 'name'));$message->setto(array('whoever@163.com' => 'mr.right', 'whoever@qq.com' => 'mr.wrong'));$message->setsubject(this is a subject);$message->setbody('here is the message', 'text/html', 'utf-8'); //'text/plain'$message->attach(swift_attachment::frompath('pic.jpg', 'image/jpeg')->setfilename('rename_pic.jpg'));try{$mailer->send($message);}catch (swift_connectionexception $e){echo 'there was a problem communicating with smtp: ' . $e->getmessage();}}
项目地址:http://swiftmailer.org/
