From cbc4fe50d4f53f34a931a3d243ccefacb27bb7f9 Mon Sep 17 00:00:00 2001 From: gzydong Date: Sat, 19 Dec 2020 18:06:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=91=E9=80=81=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Command/SendEmailCommand.php | 41 +++++++++++++ app/Support/Mail.php | 75 +++++++++++++++++++++++ composer.json | 6 +- config/autoload/mail.php | 47 ++++++-------- config/autoload/view.php | 43 +++++++++++++ config/config.php | 1 + storage/view/emails/verify-code.blade.php | 52 ++++++++++++++++ 7 files changed, 234 insertions(+), 31 deletions(-) create mode 100644 app/Command/SendEmailCommand.php create mode 100644 app/Support/Mail.php create mode 100644 config/autoload/view.php create mode 100644 storage/view/emails/verify-code.blade.php diff --git a/app/Command/SendEmailCommand.php b/app/Command/SendEmailCommand.php new file mode 100644 index 0000000..800951c --- /dev/null +++ b/app/Command/SendEmailCommand.php @@ -0,0 +1,41 @@ +container = $container; + + parent::__construct('ws:send-email'); + } + + public function configure() + { + parent::configure(); + } + + public function handle() + { + $mail = new Mail(); + $mail->sendEmailCode('837215079@qq.com', '878123', '邮件验证码标题'); + } +} diff --git a/app/Support/Mail.php b/app/Support/Mail.php new file mode 100644 index 0000000..87fd70c --- /dev/null +++ b/app/Support/Mail.php @@ -0,0 +1,75 @@ +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); + } +} diff --git a/composer.json b/composer.json index 901460e..c8c5680 100644 --- a/composer.json +++ b/composer.json @@ -33,9 +33,11 @@ "hyperf/validation": "^2.0", "phper666/jwt-auth": "^3.0", "hyperf/filesystem": "^2.0", - "hyperf/socketio-server": "^2.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": { "swoole/ide-helper": "^4.5", diff --git a/config/autoload/mail.php b/config/autoload/mail.php index 7ba4a59..293ca08 100644 --- a/config/autoload/mail.php +++ b/config/autoload/mail.php @@ -1,9 +1,21 @@ + * @link https://github.com/codingheping/hyperf-chat-upgrade + */ return [ - /* |-------------------------------------------------------------------------- | 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'), - /* - |-------------------------------------------------------------------------- - | 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'), - ], - /* - |-------------------------------------------------------------------------- - | 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. - | - */ + 'from' => env('MAIL_FROM_ADDRESS'), - 'encryption' => env('MAIL_ENCRYPTION', 'tls'), + 'name' => env('MAIL_FROM_NAME'), ]; diff --git a/config/autoload/view.php b/config/autoload/view.php new file mode 100644 index 0000000..5529429 --- /dev/null +++ b/config/autoload/view.php @@ -0,0 +1,43 @@ + 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', + ], +]; diff --git a/config/config.php b/config/config.php index f7963fe..7e4ae31 100644 --- a/config/config.php +++ b/config/config.php @@ -29,6 +29,7 @@ return [ 'web_url' => env('WEB_URL', ''),//Web 端首页地址 'img_url' => env('IMG_URL', ''),//设置文件图片访问的域名 ], + 'upload_dir'=>env('UPLOAD_PATH',''), StdoutLoggerInterface::class => [ diff --git a/storage/view/emails/verify-code.blade.php b/storage/view/emails/verify-code.blade.php new file mode 100644 index 0000000..82eb6f9 --- /dev/null +++ b/storage/view/emails/verify-code.blade.php @@ -0,0 +1,52 @@ + + + + + + + + + + + +
+
+ + +
+
+
+
+ 尊敬的用户:您好! + + 您正在进行 {{$service_name}} 操作,请在验证码输入框中输入:{{$sms_code}},以完成操作,验证码有效期15分钟。 + +
+
+ +

+ 注意:此操作可能会修改您的密码、修改邮箱或绑定手机。如非本人操作,请及时登录并修改密码以保证帐户安全 +
(工作人员不会向你索取此验证码,请勿泄漏!) +

+
+
+
+
+
+

此为系统邮件,请勿回复
+ 请保管好您的邮箱,避免账号被他人盗用 +

+

Hyperf Chat在线聊天

+
+
+
+