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

phpmailer类的再封装的更多相关文章

  1. 打印 Logger 日志时,需不需要再封装一下工具类?

    在开发过程中,打印日志是必不可少的,因为日志关乎于应用的问题排查.应用监控等.现在打印日志一般都是使用 slf4j,因为使用日志门面,有助于打印方式统一,即使后面更换日志框架,也非常方便.在 < ...

  2. 多个类的DLL封装及调用

    #define FaceLIBDLL #include "stdafx.h" #include "facedll.h" #include <opencv2 ...

  3. iOS开发之网络请求(基于AFNetworking的再封装)

    最近一直很忙也没有什么时间写博客了.放假了休息一下,就写一篇博客来总结一下最近做项目中出现过的问题吧!!! 首先,在项目中我的起到了什么作用,无非就是把美工(UI设计师)给我们的图显示出来,然后再和服 ...

  4. linux 下 用phpmailer类smtp发送邮件始终不成功,提示:ERROR: Failed to co

    https://zhidao.baidu.com/question/509191264.html?fr=iks&word=PHPMailerSMTP+connect()+failed& ...

  5. 把jQuery的类、插件封装成seajs的模块的方法

    这篇文章主要介绍了把jQuery的类.插件封装成seajs的模块的方法,需要的朋友可以参考下 注:本文使用的seajs版本是2.1.1 一.把Jquery封装成seajs的模块 define(func ...

  6. Http请求封装(对HttpClient类的进一步封装,使之调用更方便。另外,此类管理唯一的HttpClient对象,支持线程池调用,效率更高)

    package com.ad.ssp.engine.common; import java.io.IOException; import java.util.ArrayList; import jav ...

  7. 对Zlib单元进行再封装

    对Zlib单元进行再封装 低版本DELPHI,如D7,ZLIB.pas单元封装的很简陋,因此有必要再封装,以增加使用的便利性. 高版本DELPHI,zlib.pas本身提供的接口已经相当完善. Zli ...

  8. python+pytest接口自动化(11)-测试函数、测试类/测试方法的封装

    前言 在python+pytest 接口自动化系列中,我们之前的文章基本都没有将代码进行封装,但实际编写自动化测试脚本中,我们都需要将测试代码进行封装,才能被测试框架识别执行. 例如单个接口的请求代码 ...

  9. 关于Spring-JDBC测试类的简单封装

    关于Spring-JDBC测试类的简单封装 1.简单封装 /** * Created with IntelliJ IDEA. * * @Author: Suhai * @Date: 2022/04/0 ...

随机推荐

  1. Quartz使用(4) - Quartz监听器Listerner

    1. 概述 Quartz的监听器用于当任务调度中你所关注事件发生时,能够及时获取这一事件的通知.类似于任务执行过程中的邮件.短信类的提醒.Quartz监听器主要有JobListener.Trigger ...

  2. GitKraken使用教程-基础部分(3)

    5. 克隆服务器上的项目 首先,返回主界面,点击File => Clone Repo,选择 Clone with URL,如下图: 图 5‑1 SSH方式克隆仓库界面 1) SSH 方式连接仓库 ...

  3. elasticsearch复杂查询-----2

    1.多条件查询 1)查询索引weibo下字段date大于或等于2015-09-05和name为Mary Jone的数据 2.简单查询 1)查询包含2014字符的数据 2)查询包含字符2014-09-1 ...

  4. CSS知识点梳理

  5. Struts2 简介及学习方法介绍

    Struts2 =  webwork + struts1.x 尊重学习规律的操作 学习上痛苦的根源之一是只能走的时候逼我来跑 不是说深入的内容就不讲了,而是放到合适的时候讲 一段时间可以,长了集中不了 ...

  6. failed: No module named 'catkin_pkg' Make sure that you have installed "catkin_pkg"

    https://stackoverflow.com/questions/43024337/why-this-error-when-i-try-to-create-workspaces-in-ros# ...

  7. leetcode: 树

    1. sum-root-to-leaf-numbers Given a binary tree containing digits from0-9only, each root-to-leaf pat ...

  8. cesium 显示视角高度以及鼠标经纬度

    HTML中的内容 <div id="cesiumContainer"> <!-- 设置经纬度显示 --> <span style="font ...

  9. 什么是DTO?

    DTO: Data Transfer Object Wikipedia定义:Data transfer object (DTO)[1][2] is an object that carries dat ...

  10. 1993: C语言实验——最值

    1993: C语言实验——最值 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 1541  Solved: 727[Submit][Status][Web ...