<?php
namespace Common\Lib\Mailer;
/**
* 邮件类
*/
class SwiftMailer{
/**
* 邮件传输对象
* @var object
*/
private $transport;
/**
* 发送邮件配置信息
* @var array
*/
private $options = [
'from_email' => '',
'from_name' => '',
'smtp_server' => '',
'smtp_secure' => '',
'smtp_port' => 25,
'username' => '',
'password' => ''
];
/**
* 错误信息
* @var array
*/
private $error = [];
/**
* 发送邮件超时(秒)
* @var integer
*/
private $timeout = 5;
/**
* 构造方法
* @param array $options 发送邮件配置信息
* [
* from_email : 发件人邮箱
* from_name : 发件人
* smtp_server : smtp服务器
* smtp_secure : 连接方式
* smtp_port : 端口
* username : 发件箱帐号
* password : 发件箱密码
* ]
*/
public function __construct($options = [])
{
$this->options = array_merge($this->options, $options);
}
/**
* 创建实例
* @param array $options 发送邮件配置信息
* [
* from_email : 发件人邮箱
* from_name : 发件人
* smtp_server : smtp服务器
* smtp_secure : 连接方式
* smtp_port : 端口
* username : 发件箱帐号
* password : 发件箱密码
* ]
* @return this
*/
public static function create($options = [])
{
return new static($options);
}
/**
* 发送邮件接口
* @param mixed $to 收件人['xxxx@qq.com' => 'xxxx'] | 'xxxx@qq.com'
* @param string $subject 邮件主题
* @param string $body 邮件内容
* @return
*/
public function send($to, $subject, $body)
{
$transport = $this->createTransport();
$mailer = \Swift_Mailer::newInstance($transport);
//创建消息类
$message = \Swift_Message::newInstance();
try
{
//设置发件人
$message->setFrom([$this->options['from_email'] => $this->options['from_name']]);
//设置收件人
$message->setTo($to);
//设置主题
$message->setSubject($subject);
//设置主题内容
$message->setBody(htmlspecialchars_decode($body), 'text/html', 'UTF-8');
//发送邮件
$result = $mailer->send($message, $failures);
if($result) {
return true;
} else {
$this->setError('发送邮件失败', $failures);
return false;
}
}
catch(\Exception $e)
{
$this->setError($e->getMessage());
return false;
}
}
/**
* 创建邮件传输器
* @return
*/
private function createTransport()
{
$transport = \Swift_SmtpTransport::newInstance($this->options['smtp_server'], $this->options['smtp_port'], $this->options['smtp_secure']);
//设置发件箱账号、密码
$transport->setUsername($this->options['username']);
$transport->setPassword($this->options['password']);
//设置超时
$transport->setTimeout($this->timeout);
return $transport;
}
/**
* 设置发送邮件超时时间
* @param integer $timeout 发送邮件超时(秒)
*/
public function timeout($timeout)
{
$this->timeout = $timeout;
return $this;
}
/**
* 设置错误信息
* @param string $msg 错误消息
* @param array $failures 被拒绝的邮箱地址
*/
private function setError($msg, $failures = [])
{
$this->error = [
'msg' => $msg,
'failures' => $failures
];
}
/**
* 获取错误信息
* @return array
*/
public function getError()
{
return $this->error;
}
}
测试代码:
/**
* 发送邮件
* @param string $to_email 接收人邮箱
* @param string $title 主题
* @param string $content 邮件内容
* @return bool
*/
function send_email($to_email, $title, $content){
$options = [
'from_email' => C('EMAIL_FROM_EMAIL'),
'from_name' => C('EMAIL_FROM_NAME'),
'smtp_server' => C('EMAIL_SMTP_SERVER'),
'smtp_secure' => C('EMAIL_SMTP_SECURE'),
'smtp_port' => C('EMAIL_SMTP_PORT'),
'username' => C('EMAIL_USERNAME'),
'password' => C('EMAIL_PASSWORD')
];
$SwiftMailer = \Common\Lib\Mailer\SwiftMailer::create($options);
$result = $SwiftMailer->send($to_email, $title, $content);
if($result)
{
return true;
}
else
{
$errors = $SwiftMailer->getError();
$error_info = sprintf('email: (msg: %s, failures: %s)', $errors['msg'], json_encode($errors['failures']));
\Think\Log::write(mb_convert_encoding($error_info, 'UTF-8', 'GBK'), 'ERR');
return false;
}
}
//发送邮件
send_email('xxxxx@xx.com', '主题', '正文');