添加发送验证码

main
gzydong 2020-12-19 18:06:29 +08:00
parent 0d81bf4e61
commit cbc4fe50d4
7 changed files with 234 additions and 31 deletions

View File

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace App\Command;
use App\Support\Mail;
use Hyperf\Command\Annotation\Command;
use Hyperf\Command\Command as HyperfCommand;
use Psr\Container\ContainerInterface;
/**
* 测试发送验证码
*
* @Command
*/
class SendEmailCommand extends HyperfCommand
{
/**
* @var ContainerInterface
*/
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
parent::__construct('ws:send-email');
}
public function configure()
{
parent::configure();
}
public function handle()
{
$mail = new Mail();
$mail->sendEmailCode('837215079@qq.com', '878123', '邮件验证码标题');
}
}

75
app/Support/Mail.php Normal file
View File

@ -0,0 +1,75 @@
<?php
namespace App\Support;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
/**
* Class Mail
* @package App\Support
*/
class Mail
{
/**
* 发送邮件验证码
*
* @param string $eamil 邮箱地址
* @param string $sms_code 验证码
* @param string $title 邮件标题
* @return bool
*/
public function sendEmailCode(string $eamil, string $sms_code, string $title)
{
$view = $this->view(config('view.engine'), 'emails.verify-code', [
'service_name' => "Lumen IM",
'sms_code' => $sms_code,
'domain' => 'adsfas/asdfa'
]);
try {
return $this->mail($eamil, $title, $view);
} catch (\Exception $e) {
return false;
}
}
/**
* @param string $address
* @param string $subject
* @param string $view
* @return bool
* @throws \PHPMailer\PHPMailer\Exception
*/
private function mail(string $address, string $subject, string $view): bool
{
$config = config('mail');
$mail = new PHPMailer(); //PHPMailer对象
$mail->CharSet = 'UTF-8'; //设定邮件编码默认ISO-8859-1如果发中文此项必须设置否则乱码
$mail->IsSMTP(); // 设定使用SMTP服务
$mail->SMTPDebug = 0; // 关闭SMTP调试功能
$mail->SMTPAuth = true; // 启用 SMTP 验证功能
$mail->SMTPSecure = 'ssl'; // 使用安全协议
$mail->Host = $config['host']; // SMTP 服务器
$mail->Port = $config['port']; // SMTP服务器的端口号
$mail->Username = $config['username']; // SMTP; // SMTP服务器用户名
$mail->Password = $config['password']; // SMTP服务器密码
$mail->SetFrom($config['from'], $config['name']); // 邮箱,昵称
$mail->Subject = $subject;
$mail->MsgHTML($view);
$mail->AddAddress($address); // 收件人
return $mail->Send();
}
/**
* @param string $engine
* @param $template
* @param array $params
* @return string
*/
private function view(string $engine, $template, $params = []): string
{
$config = config('view.config', []);
return container()->get($engine)->render($template, $params, $config);
}
}

View File

@ -33,9 +33,11 @@
"hyperf/validation": "^2.0", "hyperf/validation": "^2.0",
"phper666/jwt-auth": "^3.0", "phper666/jwt-auth": "^3.0",
"hyperf/filesystem": "^2.0", "hyperf/filesystem": "^2.0",
"hyperf/socketio-server": "^2.0",
"hashids/hashids": "^4.0", "hashids/hashids": "^4.0",
"ext-json": "^1.6" "ext-json": "^1.6",
"hyperf/view": "^2.0",
"hyperf/view-engine": "^2.0",
"phpmailer/phpmailer": "^6.2"
}, },
"require-dev": { "require-dev": {
"swoole/ide-helper": "^4.5", "swoole/ide-helper": "^4.5",

View File

@ -1,9 +1,21 @@
<?php <?php
declare(strict_types = 1); declare(strict_types=1);
/**
*
* This file is part of the My App.
*
* Copyright CodingHePing 2016-2020.
*
* This is my open source code, please do not use it for commercial applications.
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
*
* @author CodingHePing<847050412@qq.com>
* @link https://github.com/codingheping/hyperf-chat-upgrade
*/
return [ return [
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| SMTP Host Address | SMTP Host Address
@ -28,7 +40,7 @@ return [
| |
*/ */
'port' => env('MAIL_PORT', 587), 'port' => env('MAIL_PORT', 465),
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@ -45,32 +57,9 @@ return [
'password' => env('MAIL_PASSWORD'), 'password' => env('MAIL_PASSWORD'),
/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here, you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
/* 'from' => env('MAIL_FROM_ADDRESS'),
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/
'encryption' => env('MAIL_ENCRYPTION', 'tls'), 'name' => env('MAIL_FROM_NAME'),
]; ];

43
config/autoload/view.php Normal file
View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
use Hyperf\View\Mode;
use Hyperf\ViewEngine\HyperfViewEngine;
return [
'engine' => HyperfViewEngine::class,
'mode' => Mode::SYNC,
'config' => [
'view_path' => BASE_PATH . '/storage/view/',
'cache_path' => BASE_PATH . '/runtime/view/',
'charset' => 'UTF-8',
],
// Autoload components.
'autoload' => [
'classes' => [
'App\\View\\Component\\',
],
'components' => [
'components.', // BASE_PATH . '/storage/view/components/'
],
],
# Custom components.
'components' => [
// 'other-alert' => \Other\ViewComponent\Alert::class
],
# View namespaces. (Used for packages)
'namespaces' => [
// 'admin' => BASE_PATH . '/storage/view/vendor/admin',
],
];

View File

@ -29,6 +29,7 @@ return [
'web_url' => env('WEB_URL', ''),//Web 端首页地址 'web_url' => env('WEB_URL', ''),//Web 端首页地址
'img_url' => env('IMG_URL', ''),//设置文件图片访问的域名 'img_url' => env('IMG_URL', ''),//设置文件图片访问的域名
], ],
'upload_dir'=>env('UPLOAD_PATH',''), 'upload_dir'=>env('UPLOAD_PATH',''),
StdoutLoggerInterface::class => [ StdoutLoggerInterface::class => [

View File

@ -0,0 +1,52 @@
<head>
<base target="_blank" />
<style type="text/css">
body{font-size:14px;font-family:arial,verdana,sans-serif;line-height:1.666;padding:0;margin:0;overflow:auto;white-space:normal;word-wrap:break-word;min-height:100px}
td, input, button, select, body{font-family:Helvetica, 'Microsoft Yahei', verdana}
pre {white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;width:95%}
th,td{font-family:arial,verdana,sans-serif;line-height:1.666}
img{ border:0}
header,footer,section,aside,article,nav,hgroup,figure,figcaption{display:block}
blockquote{margin-right:0px}
::-webkit-scrollbar{ display: none; }
</style>
</head>
<body tabindex="0" role="listitem">
<table width="700" border="0" align="center" cellspacing="0" style="width:700px;">
<tbody>
<tr>
<td>
<div style="width:700px;margin:0 auto;border-bottom:1px solid #ccc;margin-bottom:30px;">
<table border="0" cellpadding="0" cellspacing="0" width="700" height="39" style="font:12px Tahoma, Arial, 宋体;">
<tbody><tr><td width="210"></td></tr></tbody>
</table>
</div>
<div style="width:680px;padding:0 10px;margin:0 auto;">
<div style="line-height:1.5;font-size:14px;margin-bottom:25px;color:#4d4d4d;">
<strong style="display:block;margin-bottom:15px;">尊敬的用户:<span style="color:#f60;font-size: 16px;"></span>您好!</strong>
<strong style="display:block;margin-bottom:15px;">
您正在进行 <span style="color: red">{{$service_name}}</span> 操作,请在验证码输入框中输入:<span style="color:#f60;font-size: 24px">{{$sms_code}}</span>,以完成操作,验证码有效期<span style="color:#f60;font-size: 24px">15</span>分钟。
</strong>
</div>
<div style="margin-bottom:30px;">
<small style="display:block;margin-bottom:20px;font-size:12px;">
<p style="color:#747474;">
注意:此操作可能会修改您的密码、修改邮箱或绑定手机。如非本人操作,请及时登录并修改密码以保证帐户安全
<br>(工作人员不会向你索取此验证码,请勿泄漏!)
</p>
</small>
</div>
</div>
<div style="width:700px;margin:0 auto;">
<div style="padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;">
<p>此为系统邮件,请勿回复<br>
请保管好您的邮箱,避免账号被他人盗用
</p>
<p style="margin-top: 15px"><a href="{{$domain}}" style="text-decoration: none;color: #3f99e6;">Hyperf Chat在线聊天</a></p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</body>