Swift Mailer PHP邮件类

原创
2016-06-20 13:02:28 1315浏览

一、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/


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。