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. Python札记1--基础

    Python语言虽然简单,但相关的细节仍需要注意,本系列札记,旨在记录学习python过程中需要注意的相关知识点或相关难点的理解. 1. 列表 a. python的列表list中元素的类型可以不同 b ...

  2. MVC视图之间调用方法总结

    调用视图方式有两种: 一:直接调用(不通过controller) <body> @Html.Partial("../Shared/Error"); </body& ...

  3. SQL简单语句(增删改查)

    简单的SQL语句增删改查操作 说明: 在mysql里面亲测结果正确    用到的表(学生表:studnets) 1.创建一个学生表,(学号,姓名,性别,家庭住址) mysql> create t ...

  4. [LeetCode]11. Container With Most Water 盛最多水的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  5. C#中关于静态与非静态的一个疑问

    关于静态方法.变量和非静态方法.变量的区别,园里的大神早就有了许多详细的总结,个人觉得静态方法.变量与非静态方法.变量的区别可以总结为以下两句话: 静态的是属于类的 非静态是属于对象的 就是说调用静态 ...

  6. 在mac上使用github for mac 创建并上传项目

    1.下载github for mac https://mac.github.com/ 2.登陆 偏好设置 3.用Xcode 创建一个项目,勾上“create local git respository ...

  7. bootstrap导航栏的辛酸史

    昨天本来想完成test10的页面内容的,但是给老铁拉出去打麻将呢.不过还好昨天写了一些内容.现在奉上.不作更改. 今天完成的事情:(实现了test9的响应式导航栏的垂直平分和下拉列表的居中问题.) 我 ...

  8. TeeChart for .NET常用属性总结

    本文总结了图表控件Teechart for .NET常用的一些属性,对图表开发人员来说是一个很好的参考. 原文链接:http://blog.csdn.net/u010270772/article/de ...

  9. SAP R/3 IDES 4.71 编译前后硬盘空间大小比较

    使用SGEN编译前 使用SGEN编译后

  10. u-boot分析(六)----时钟初始化

    u-boot分析(六) 上篇博文我们按照210的启动流程,分析到了关闭看门狗,今天我们继续按照u-boot的启动流程进行分析,今天我们会主要分析时钟的初始化. 今天我们会用到的文档: 1.       ...