用到了swiftmailer 的lib库
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的特点:
- Send emails using SMTP, sendmail, postfix or a custom Transport implementation of your own
- Support servers that require username & password and/or encryption
- Protect from header injection attacks without stripping request data content
- Send MIME compliant HTML/multipart emails
- Use event-driven plugins to customize the library
- Handle large attachments and inline/embedded images with low memory use