发送邮件
This commit is contained in:
parent
624a2628d0
commit
5022d1f3d7
@ -3,6 +3,7 @@
|
||||
namespace app\controller;
|
||||
|
||||
use app\model\User;
|
||||
use app\service\MailService;
|
||||
use app\util\JwtUtil;
|
||||
use app\util\Util;
|
||||
use support\Redis;
|
||||
@ -13,7 +14,7 @@ class UserController
|
||||
/**
|
||||
* 不需要登录的方法
|
||||
*/
|
||||
protected $noNeedLogin = ['login'];
|
||||
protected $noNeedLogin = ['login','sendCode'];
|
||||
|
||||
public function login(Request $request)
|
||||
{
|
||||
@ -59,5 +60,35 @@ class UserController
|
||||
return Util::success($result,trans('login_success'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送邮箱验证码
|
||||
* @param Request $request
|
||||
* @return \support\Response
|
||||
* @throws \Random\RandomException
|
||||
*/
|
||||
public function sendCode(Request $request)
|
||||
{
|
||||
// $email = $request->input('email');
|
||||
$email = '1696136552@qq.com';
|
||||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
return json(['code' => 400, 'msg' => '邮箱格式错误']);
|
||||
}
|
||||
|
||||
// 生成验证码
|
||||
$code = str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
|
||||
|
||||
// 发送邮件
|
||||
$mailService = new MailService();
|
||||
if ($mailService->sendVerificationCode($email, $code)) {
|
||||
// 存储验证码到 Redis
|
||||
Redis::set("verification_code:{$email}", $code, 300);
|
||||
return Util::success([],'验证码已发送');
|
||||
}
|
||||
|
||||
return Util::fail([],'邮件发送失败');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -58,7 +58,11 @@ class AuthMiddleware implements MiddlewareInterface
|
||||
if ($isLogin && (!$token || !str_starts_with($token, 'Bearer '))) {
|
||||
throw new BusinessException('请先登录', 302);
|
||||
// throw new BusinessException('未提供 Token'.$isLogin, 401);
|
||||
}elseif(!$isLogin){
|
||||
return $handler($request);
|
||||
}
|
||||
|
||||
|
||||
$token = substr($token, 7);
|
||||
try {
|
||||
// 验证 Token 并获取用户ID
|
||||
|
58
webman/app/service/MailService.php
Normal file
58
webman/app/service/MailService.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
namespace app\service;
|
||||
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
use support\Log;
|
||||
|
||||
class MailService
|
||||
{
|
||||
protected $mailer;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->mailer = new PHPMailer(true);
|
||||
$this->configure();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
// SMTP 配置
|
||||
$this->mailer->isSMTP();
|
||||
$this->mailer->Host = 'smtp.qq.com';
|
||||
$this->mailer->SMTPAuth = true;
|
||||
$this->mailer->Username = '1696136552@qq.com'; // 替换为你的 Gmail
|
||||
$this->mailer->Password = 'jhqkgjwzqqzgdcfd'; // 替换为应用专用密码
|
||||
// $this->mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // 使用 TLS
|
||||
$this->mailer->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // SSL 加密
|
||||
// $this->mailer->Port = 587;
|
||||
$this->mailer->Port = 465;
|
||||
$this->mailer->CharSet = 'UTF-8';
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送验证码邮件
|
||||
* @param string $toEmail 收件邮箱
|
||||
* @param string $code 验证码
|
||||
* @return bool
|
||||
*/
|
||||
public function sendVerificationCode(string $toEmail, string $code): bool
|
||||
{
|
||||
try {
|
||||
$this->mailer->setFrom('1696136552@qq.com', 'XinYouHe Code'); // 发件人
|
||||
$this->mailer->addAddress($toEmail);
|
||||
|
||||
// 邮件内容
|
||||
$this->mailer->isHTML(true);
|
||||
$this->mailer->Subject = '您的验证码';
|
||||
$this->mailer->Body = "您的CRM验证码是:<strong>{$code}</strong>,5分钟内有效。";
|
||||
|
||||
$this->mailer->send();
|
||||
Log::info("邮件发送成功: {$toEmail}");
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
Log::error("邮件发送失败: " . $this->mailer->ErrorInfo);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -34,7 +34,8 @@
|
||||
"webman/redis": "^2.1",
|
||||
"symfony/translation": "^7.2",
|
||||
"firebase/php-jwt": "^6.11",
|
||||
"illuminate/database": "^11.44"
|
||||
"illuminate/database": "^11.44",
|
||||
"phpmailer/phpmailer": "^6.9"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-event": "For better performance. "
|
||||
|
83
webman/composer.lock
generated
83
webman/composer.lock
generated
@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "f3ffc56f1691457e45c7053ac636e4be",
|
||||
"content-hash": "55d46d79b19fab3d24ee06892f254c93",
|
||||
"packages": [
|
||||
{
|
||||
"name": "brick/math",
|
||||
@ -1261,6 +1261,87 @@
|
||||
},
|
||||
"time": "2018-02-13T20:26:39+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phpmailer/phpmailer",
|
||||
"version": "v6.9.3",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/PHPMailer/PHPMailer.git",
|
||||
"reference": "2f5c94fe7493efc213f643c23b1b1c249d40f47e"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/2f5c94fe7493efc213f643c23b1b1c249d40f47e",
|
||||
"reference": "2f5c94fe7493efc213f643c23b1b1c249d40f47e",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-ctype": "*",
|
||||
"ext-filter": "*",
|
||||
"ext-hash": "*",
|
||||
"php": ">=5.5.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
|
||||
"doctrine/annotations": "^1.2.6 || ^1.13.3",
|
||||
"php-parallel-lint/php-console-highlighter": "^1.0.0",
|
||||
"php-parallel-lint/php-parallel-lint": "^1.3.2",
|
||||
"phpcompatibility/php-compatibility": "^9.3.5",
|
||||
"roave/security-advisories": "dev-latest",
|
||||
"squizlabs/php_codesniffer": "^3.7.2",
|
||||
"yoast/phpunit-polyfills": "^1.0.4"
|
||||
},
|
||||
"suggest": {
|
||||
"decomplexity/SendOauth2": "Adapter for using XOAUTH2 authentication",
|
||||
"ext-mbstring": "Needed to send email in multibyte encoding charset or decode encoded addresses",
|
||||
"ext-openssl": "Needed for secure SMTP sending and DKIM signing",
|
||||
"greew/oauth2-azure-provider": "Needed for Microsoft Azure XOAUTH2 authentication",
|
||||
"hayageek/oauth2-yahoo": "Needed for Yahoo XOAUTH2 authentication",
|
||||
"league/oauth2-google": "Needed for Google XOAUTH2 authentication",
|
||||
"psr/log": "For optional PSR-3 debug logging",
|
||||
"symfony/polyfill-mbstring": "To support UTF-8 if the Mbstring PHP extension is not enabled (^1.2)",
|
||||
"thenetworg/oauth2-azure": "Needed for Microsoft XOAUTH2 authentication"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"PHPMailer\\PHPMailer\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"LGPL-2.1-only"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Marcus Bointon",
|
||||
"email": "phpmailer@synchromedia.co.uk"
|
||||
},
|
||||
{
|
||||
"name": "Jim Jagielski",
|
||||
"email": "jimjag@gmail.com"
|
||||
},
|
||||
{
|
||||
"name": "Andy Prevost",
|
||||
"email": "codeworxtech@users.sourceforge.net"
|
||||
},
|
||||
{
|
||||
"name": "Brent R. Matzelle"
|
||||
}
|
||||
],
|
||||
"description": "PHPMailer is a full-featured email creation and transfer class for PHP",
|
||||
"support": {
|
||||
"issues": "https://github.com/PHPMailer/PHPMailer/issues",
|
||||
"source": "https://github.com/PHPMailer/PHPMailer/tree/v6.9.3"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://github.com/Synchro",
|
||||
"type": "github"
|
||||
}
|
||||
],
|
||||
"time": "2024-11-24T18:04:13+00:00"
|
||||
},
|
||||
{
|
||||
"name": "psr/clock",
|
||||
"version": "1.0.0",
|
||||
|
Loading…
x
Reference in New Issue
Block a user