Swift Mailer PHP邮件类

WBOY
Release: 2016-06-20 13:02:28
Original
1816 people have browsed it

一、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();
}
}
Copy after login

项目地址:http://swiftmailer.org/


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!