Skip to content

Commit

Permalink
增加简易邮件发送类
Browse files Browse the repository at this point in the history
  • Loading branch information
He110te4m committed Feb 18, 2018
1 parent f41d0dc commit e490811
Show file tree
Hide file tree
Showing 71 changed files with 9,285 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/Home/controller/Index.php
Expand Up @@ -17,6 +17,6 @@ class Index extends Controller
{
public function index()
{
// $this->render('index.php');
$this->render('index.php');
}
}
6 changes: 4 additions & 2 deletions composer.json
Expand Up @@ -5,11 +5,13 @@
"version": "3.0.1",
"require": {
"php": ">=5.5.0",
"ext-PDO": "*"
"ext-PDO": "*",
"phpmailer/phpmailer": "^6.0"
},
"require-dev": {
"php": ">=5.5.0",
"ext-PDO": "*"
"ext-PDO": "*",
"phpmailer/phpmailer": "^6.0"
},
"license": "GPL-3.0-or-later",
"authors": [
Expand Down
27 changes: 26 additions & 1 deletion despote/conf/services.php
Expand Up @@ -26,7 +26,7 @@
// 'action' => 'index',
// ],
// MySQL
'sql' => [
'sql' => [
'class' => '\despote\kernel\db\MySQL',

// // 数据库地址,默认为 localhost
Expand Down Expand Up @@ -141,4 +141,29 @@
// // 加密的密钥
// 'secret' => 'Despote',
// ],
'mail' => [
'class' => '\despote\extend\Mailer',
// // 使用的安全协议
// 'SMTPSecure' => 'ssl',
// // 邮件的字符编码
// 'charset' => 'UTF-8',
// // 是否进行安全认证
// 'SMTPAuth' => true,
// // 邮件服务器端口
// 'port' => 25,
// // 邮件服务器地址
// 'host' => 'smtp.exmail.qq.com',
// // 邮件服务器登陆的用户名
// 'user' => 'i@he110.top',
// // 邮件服务器登陆的密码
// 'pwd' => 'test',
// // 发送人邮箱
// 'form' => 'i@he110.top',
// // 发送人姓名
// 'formname' => 'He110',
// // 回复邮箱
// 'replyto' => 'i@he110.top',
// // 回复姓名
// 'replyname' => 'He110',
],
];
136 changes: 136 additions & 0 deletions despote/extend/Mailer.php
@@ -0,0 +1,136 @@
<?php
/*
* ____ _
* | _ \ ___ ___ _ __ ___ | |_ ___
* | | | |/ _ \/ __| '_ \ / _ \| __/ _ \
* | |_| | __/\__ \ |_) | (_) | || __/
* |____/ \___||___/ .__/ \___/ \__\___|
* |_|
* 简易封装的邮件发送类,基于 PHPMailer
* @author He110 (i@he110.top)
* @namespace
* @example
*/

namespace despote\extend;

use \despote\base\Service;
use \PHPMailer\PHPMailer\PHPMailer;

class Mailer extends Service
{
// 单例 PHPMailer 类
private $obj = null;

// 使用的安全协议
protected $SMTPSecure = 'ssl';
// 邮件的字符编码
protected $CharSet = 'UTF-8';
// 是否进行安全认证
protected $SMTPAuth = true;
// 邮件服务器端口
protected $Port = 465;
// 邮件服务器地址
protected $Host = 'smtp.exmail.qq.com';
// 邮件服务器登陆的用户名
protected $User = 'i@he110.top';
// 邮件服务器登陆的密码
protected $Pwd = 'test';
// 发送人邮箱
protected $Form = 'i@he110.top';
// 发送人姓名
protected $FormName = 'He110';
// 回复邮箱
protected $ReplyTo = 'i@he110.top';
// 回复姓名
protected $ReplyName = 'He110';

public function init()
{
empty($this->obj) && $this->obj = new PHPMailer(false);
}

public function config($c)
{
// 使用的安全协议
$this->SMTPSecure = isset($c['SMTPSecure']) ? $c['SMTPSecure'] : $this->SMTPSecure;
// 邮件的字符编码
$this->CharSet = isset($c['CharSet']) ? $c['CharSet'] : $this->CharSet;
// 是否进行安全认证
$this->SMTPAuth = isset($c['smtpauth']) && !empty($c['smtpauth']) ? true : $this->SMTPAuth;
// 邮件服务器端口
$this->Port = isset($c['Port']) ? $c['Port'] : $this->Port;
// 邮件服务器地址
$this->Host = isset($c['Host']) ? $c['Host'] : $this->Host;
// 邮件服务器登陆的用户名
$this->User = isset($c['User']) ? $c['User'] : $this->User;
// 邮件服务器登陆的密码
$this->Pwd = isset($c['Pwd']) ? $c['Pwd'] : $this->Pwd;
// 发送人邮箱
$this->Form = isset($c['formmail']) ? $c['formmail'] : $this->Form;
// 发送人姓名
$this->FormName = isset($c['FormName']) ? $c['FormName'] : $this->FormName;
// 回复邮箱
$this->ReplyTo = isset($c['ReplyTo']) ? $c['ReplyTo'] : $this->ReplyTo;
// 回复姓名
$this->ReplyName = isset($c['ReplyName']) ? $c['ReplyName'] : $this->ReplyName;
}

public function send($to, $title, $body, $file = null, $length = 80)
{
// 获取 PHPMailer 对象
$mail = &$this->obj;

// 设置安全协议
if (!empty($this->SMTPSecure)) {
$mail->SMTPSecure = $this->SMTPSecure;
}
// 设置编码
$mail->CharSet = $this->CharSet;
// 使用 base64 加密
$mail->Encoding = 'base64';
// 使用 SMTP 方式
$mail->IsSMTP();
// 设置端口
$mail->Port = $this->Port;
// 设置服务端地址
$mail->Host = $this->Host;
// 设置服务器认证配置
$mail->SMTPAuth = $this->SMTPAuth;
// 是否开启 Debug 模式
$mail->SMTPDebug = \Utils::config('error_catch', false);
// 设置发件人邮箱
$mail->Username = $this->User;
// 设置发件人密码
$mail->Password = $this->Pwd;
// 发送的邮箱
$mail->From = $this->Form;
// 发送人
$mail->FromName = $this->FormName;
// 设置回复地址
$mail->AddReplyTo($this->ReplyTo, $this->ReplyName);
// 添加接收人
$mail->AddAddress($to);
// 设置邮件标题
$mail->Subject = $title;
// 设置邮件内容
$mail->Body = $body;
// 设置邮件每行字符串长度
$mail->WordWrap = $length;
// 如果有附件就设置附件,如果是数组循环设置附件
if (!empty($file)) {
if (is_array($file)) {
foreach ($file as $item) {
$mail->AddAttachment($item);
}
} else {
$mail->AddAttachment($file);
}
}
// 发送 HTML
$mail->IsHTML(true);

// 返回发送结果
return $mail->Send();
}
}
4 changes: 3 additions & 1 deletion despote/init.php
Expand Up @@ -32,6 +32,8 @@
define('PATH_CONF', PATH_DESPOTE . 'conf' . DS);
// 框架核心文件目录
define('PATH_KERNEL', PATH_DESPOTE . 'kernel' . DS);
// 框架扩展文件目录
define('PATH_EXTEND', PATH_DESPOTE . 'extend' . DS);

// 缓存目录
define('PATH_CACHE', PATH_DESPOTE . 'runtime' . DS . 'cache' . DS);
Expand All @@ -41,5 +43,5 @@
// 定义视图文件访问校验
define('DESPOTE', true);

// 支持 composer 自动加载
// 使用 composer 自动加载
file_exists(PATH_ROOT . 'vendor' . DS . 'autoload.php') && require PATH_ROOT . 'vendor' . DS . 'autoload.php';
7 changes: 7 additions & 0 deletions vendor/autoload.php
@@ -0,0 +1,7 @@
<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitac6379e528a17668cd9675ec0567996e::getLoader();

0 comments on commit e490811

Please sign in to comment.