phpmailer类的再封装

email


<?php use PHPMailer\PHPMailer\PHPMailer; class Email
{
const SMTPDebug = 2;
const HOST = 'smtp.qq.com';
const USERNAME = 'username';
const PASSOWRD = 'password';
const CHARSET = 'UTF-8';
const SMTPAUTH = true;
const ISHTML = true;
const SMTPSECURE = 'tls';
const PORT = 587;
static $addressee = array();
static $sender = array();
static $cc = array();
static $bcc = array();
static $attachment= array();
static $title = '';
static $body = '';
static $altBody = '';
private $mail = null; function __construct()
{
$this->mail = new PHPMailer();
} //初始化参数
public function start()
{
$this->mail->SMTPDebug = self::SMTPDebug;
$this->mail->Host = self::HOST;
$this->mail->CharSet = self::CHARSET;
$this->mail->SMTPAuth = self::SMTPAUTH;
$this->mail->Username = self::USERNAME;
$this->mail->Password = self::PASSOWRD;
$this->mail->SMTPSecure = self::SMTPSECURE;
$this->mail->Port = self::PORT;
$this->mail->isHTML(self::ISHTML);
} //发送人
public function setSender($address,$senderName = '')
{
self::$sender['a'] = $address;
self::$sender['n'] = $senderName;
} //收件人回复的地址
public function setReplyAddress($address = '', $replayName = '')
{
self::$sender['a'] = $address;
self::$sender['n'] = $replayName;
} //单个接收人
public function setAddressee($address,$recName = '')
{
call_user_func_array([$this, 'setAddress'],['addressee',$address, $recName]);
} //多个接收人
public function setManyAddressee($array)
{
array_walk($array,function($v, $k){
$this->setAddressee($v);
});
} //抄送单个
public function setCC($address = '', $name = '')
{
call_user_func_array([$this, 'setAddress'],['cc',$address, $name]);
} //抄送多个
public function setManyCC($array)
{
array_walk($array,function($v, $k){
$this->setCC($v);
});
} //暗抄送单个
public function setBCC($address = '', $name = '')
{
call_user_func_array([$this, 'setAddress'],['bcc',$address, $name]);
} //暗抄送单个
public function setManyBCC($array)
{
array_walk($array,function($v, $k){
$this->setBCC($v);
});
} //附件单个
public function setAttachment($address, $newName = '')
{
call_user_func_array([$this, 'setAddress'],['attachment',$address, $newName]);
} //附件多个
public function setManyAttachment($array)
{
array_walk($array,function($v, $k){
$this->setAttachment($v);
});
} public function setAddress($param, $address, $name)
{
array_push(self::${$param}, [
'a'=>$address,'n'=>$name
]
);
} public function setContent($title = '', $body='', $altBody='')
{
self::$title = $title;
self::$body = $body;
self::$altBody = $altBody;
} //发送邮件
public function send()
{
try {
$this->mail->isSMTP(); $this->start(); $this->mail->setFrom(self::$sender['a'], self::$sender['n']);
$this->mail->addReplyTo(self::$sender['a'], self::$sender['n']); $this->addAddress(self::$addressee,'addAddress');
$this->addAddress(self::$bcc,'addBCC');
$this->addAddress(self::$cc,'addCC');
$this->addAddress(self::$attachment,'addAttachment'); $this->mail->Subject = self::$title;
$this->mail->Body = self::$body;
$this->mail->AltBody = self::$altBody;
$this->mail->send(); $this->mail->isSMTP();
}catch (Exception $e){
echo 'Mailer Error: ' . $this->mail->ErrorInfo;
}
} public function addAddress($address,$func)
{
foreach ($address as $item){
$item['a'] && $this->mail->{$func}($item['a'], $item['n']);
}
} } $a = new Email();
$a->setSender('xxxxx@qq.com','发送者');//发送人
$a->setManyAddressee(['xxxxxxx@qq.com','aaaa@qq.com']);//多个收件人
$a->setManyAttachment(['a.jpg','b.jpg']);//多个附件
$a->setManyCC(['jjj@163.com']);//抄送
$a->setContent('我是标题','<h2>我是内容</h2>');
$a->send();
上一篇:【原创】大数据基础之ElasticSearch(3)升级


下一篇:Oracle GoldenGate微服务架构的服务Shell脚本