> 安装 : composer require phpmailer/phpmailer
*****
`
~~~
/**
* 设备或配置系统参数
* @param string $name 参数名称
* @return string|boolean
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
function GetSysConfig($name)
{
$key = md5(config('database.hostname').'#'.config('database.database').$name);
if (Cache::has($key)) {
$data = Cache::get($key);
} else {
$data = Db::name('setting')->where("key", "=", "{$name}")->value("value");
Cache::set($key, $data, 180);
}
return $data;
}
~~~
`
`
~~~
/**
* @param $subject
* @param $body
* @param $to
* @param null $attachment
* @return array
* @throws \PHPMailer\PHPMailer\Exception
* @throws \think\Exception
* @throws \think\exception\PDOException
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/17
* @name: sendMaile
* @describe: 邮件发送
*/
function sendMaile($subject, $body, $to, &$attachment = null)
{
$maile = Email::instance();
$maile->subject($subject);
$maile->body($body);
$maile->to($to);
if (isset($attachment) && !empty($attachment)) {
if (is_array($attachment)) {
foreach ($attachment as $v) {
$maile->attachment($v);
}
} else {
$maile->attachment($attachment);
}
}
$result = $maile->send();
if ($result) {
$arr = ['code' => 1, 'msg' => '发送成功'];
} else {
$arr = ['code' => 0, 'msg' => $maile->getError()];
}
return $arr;
}
}
~~~
`
`
~~~
<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/1/9-9:19
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
namespace Jrk;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require VENDOR_PATH.'autoload.php';
class Email
{
/**
* 单例对象
*/
protected static $instance;
/**
* phpmailer对象
*/
protected $mail = [];
/**
* 错误内容
*/
protected $_error = '';
/**
* 默认配置
*/
public $options = [
'charset' => 'utf-8', //编码格式
'debug' => 0, //调式模式
];
/**
* @param array $options
* @return static
* @throws \think\Exception
* @throws \think\exception\PDOException
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/14 0014
* @name: instance
* @describe:
*/
public static function instance($options = [])
{
if (is_null(self::$instance)) {
self::$instance = new static($options);
}
return self::$instance;
}
/**
* Email constructor.
* @param array $options
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function __construct($options = [])
{
$this->options = array_merge($this->options, $options);
$securArr = [1 => 'tls', 2 => 'ssl'];
$this->mail = new PHPMailer(true);
$this->mail->CharSet = $this->options['charset'];
$this->mail->SMTPDebug = $this->options['debug'];
$this->mail->isSMTP();
$this->mail->SMTPAuth = true;
$this->mail->Host = GetSysConfig('mail_smtp_host');
$this->mail->Username = GetSysConfig('mail_smtp_user');
$this->mail->Password = GetSysConfig('mail_smtp_pass');
$this->mail->SMTPSecure = isset($securArr[GetSysConfig('mail_verify_type')]) ? $securArr[GetSysConfig('mail_verify_type')] : '';
$this->mail->Port = GetSysConfig('mail_smtp_port');
//设置发件人
$this->from(GetSysConfig('mail_smtp_user'), GetSysConfig('mail_from_name'));
}
/**
* @param $subject
* @return $this
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/1/9
* @name: subject
* @describe:设置邮件主题
*/
public function subject($subject)
{
$this->options['subject'] = $subject;
return $this;
}
/**
* @param $file
* @return $this
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/1/9
* @name: addAttachment
* @describe:添加附件
*/
public function attachment($file){
$this->options['attachment'] = $file;
return $this;
}
/**
* @param $email
* @param string $name
* @return $this
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/1/9
* @name: from
* @describe:设置发件人
*/
public function from($email, $name = '')
{
$this->options['from'] = $email;
$this->options['from_name'] = $name;
return $this;
}
/**
* @param $email
* @param string $name
* @return $this
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/1/9
* @name: to
* @describe:设置收件人
*/
public function to($email, $name = '')
{
$this->options['to'] = $email;
$this->options['to_name'] = $name;
return $this;
}
/**
* @param $body
* @param bool $ishtml
* @return $this
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/1/9
* @name: message
* @describe:设置邮件正文
*/
public function body($body, $ishtml = true)
{
$this->options['body'] = $body;
$this->options['ishtml'] = $ishtml;
return $this;
}
/**
* @return string
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/1/9
* @name: getError
* @describe:获取最后产生的错误
*/
public function getError()
{
return $this->_error;
}
/**
* @param $error
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/1/9
* @name: setError
* @describe:设置错误
*/
protected function setError($error)
{
$this->_error = $error;
}
/**
* @return bool
* @throws Exception
* @throws \think\Exception
* @throws \think\exception\PDOException
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/14 0014
* @name: send
* @describe: 发送邮件
*/
public function send()
{
$result = false;
switch (GetSysConfig('mail_type')) {
case 1:
//使用phpmailer发送
$this->mail->setFrom($this->options['from'], $this->options['from_name']);
$this->mail->addAddress($this->options['to'], $this->options['to_name']);
if (isset($this->options['attachment'])){
$this->mail->addAttachment($this->options['attachment']);
}
$this->mail->Subject = $this->options['subject'];
if ($this->options['ishtml']) {
$this->mail->msgHTML($this->options['body']);
} else {
$this->mail->Body = $this->options['body'];
}
try {
$result = $this->mail->send();
} catch (\Exception $e) {
$this->setError($e->getMessage());
}
$this->setError($result ? '' : $this->mail->ErrorInfo);
break;
case 2:
//使用mail方法发送邮件
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text/html; charset=" . $this->options['charset'] . "\r\n";
$headers .= "To: {$this->options['to_name']} <{$this->options['to']}>\r\n"; //收件人
$headers .= "From: {$this->options['from_name']} <{$this->options['from']}>\r\n"; //发件人
$result = mail($this->options['to'], $this->options['subject'], $this->options['body'], $headers);
$this->setError($result ? '' : error_get_last()['message']);
break;
default:
//邮件功能已关闭
$this->setError('Mail already closed');
break;
}
return $result;
}
}
~~~
`
- 空白目录
- thinkphp5
- tools-常用类库
- redis类库
- Excel类库
- File文件操作类库
- Http请求类库
- Maile邮件发送
- Hooks行为钩子
- 七牛云
- 随机数和字符串生成
- 字符串处理
- 时间类处理
- tree型转换
- 工具类库
- 文件打包下载
- 常用功能
- 文件上传
- php生成word文档
- elasticsearch 基本搜索
- 使用jwt开发API接口
- 安装模及搭建
- ApiCheck.php
- ApiCheckLogin.php
- common.php
- Login.php
- Comment.php
- 汉字转拼音
- 安装使用
- Pinyin类
- elasticsearch操作
- 常用方法
- 数据源生成layui-select
- 获取自定义配置项
- 百度编辑器
- 格式化文件大小
- 多语言设置
- hook监听
- 域名绑定到模块
- thinkphp6
- 文件上传
- tp5totp6
- 创建路径
- 获取类所有方法
- password_hash加密验证
- 生成 qrcode
- 邮件发送
- 获取QQ信息
- GoogleAuthenticator
- redis限流
- redis 加锁
- 百度翻译
- QueryList爬取数据
- 获取时间类
- 命令
- Git常用命令
- easyswoole
- pix_qrcode
- 验证 cpf,cnpj
- php常用方法
- 日志
- 卡通头像
- 两位小数
- 图片转base64
- auth加密解密
- phpoffice/phpspreadsheet导入导出
- fastadmin
- 树结构
- 单选框
- 复选框
- 二级搜索
- select选择框
- selectpage选中回调事件
- 标签添加
- 修改where条件
- 表格列表中添加input框
- selectpage事件
- fieldlist
- js操作
- test_js
- 多表格
- template模板