PHP使用SwiftMailer发送邮件

用到了swiftmailer 的lib库

官方网址:http://swiftmailer.org/

require_once 'lib/swift_required.php';

//给我发送邮件

function mailer(){

$body ="要发送的内容";

$subject = "要发送主题"

$transport = Swift_SmtpTransport::newInstance('smtp.qq.com', 25);//设置发送 smtp 服务器和端口

$transport->setUsername('发送邮箱 地址 ');

$transport->setPassword('发送邮箱密码');

$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance();

$message->setFrom(array( '发送邮箱地址' =>'发送人'));

$message->setTo(array('接受邮箱地址' => '接受人'));

$message->setSubject($subject);

$message->setBody($body, 'text/html', 'utf-8');

//$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();

}

}

Swift 直接与 SMTP 服务器通讯,具有非常高的发送速度和效率。

Swiftmailer的特点:

  1. Send emails using SMTP, sendmail, postfix or a custom Transport implementation of your own
  2. Support servers that require username & password and/or encryption
  3. Protect from header injection attacks without stripping request data content
  4. Send MIME compliant HTML/multipart emails
  5. Use event-driven plugins to customize the library
  6. Handle large attachments and inline/embedded images with low memory use