Hyperf2.1框架中使用hyperf-ext/mail组件包发送邮件流程步骤
swoole  /  管理员 发布于 2年前   3577
最近有个hyperf框架的项目要加个发送邮件的功能,在网上一顿操作找轮子,所以就找到这个组件包
为什么用它,主要是我是laravel爱好者,看到了它的介绍所以我直接就用了,因为我之前用过laravel发邮件,也写过博客,需要的自行在本站laravel类里面搜索一下
很熟悉的介绍:
该组件衍生自 illuminate/mail,基于 SwiftMailer 函数库提供了一套干净、简洁的 API ,可以为 SMTP、Mailgun、Postmark、AWS SES、阿里云 DM 和 sendmail 提供驱动,让你可以快速从本地或云端服务自由地发送邮件。
环境还是之前的环境啊,线上体验链接:https://blog.zongscan.com/ 这个
不说了,正式安装 走流程
安装
composer require hyperf-ext/mail
发布配置
php bin/hyperf.php vendor:publish hyperf-ext/mail
它有好几个驱动可以选择,我这里直接用默认的smtp
发送邮箱163邮箱 (怎么开通smtp/pop3服务,怎么拿到授权码这里我就不说了,以前的文章有写)
配置文件 添加邮箱信息 根目录的.env文件
MAIL_MAILER=smtp
MAIL_SMTP_HOST=smtp.163.com
MAIL_SMTP_PORT=465
[email protected]
MAIL_SMTP_PASSWORD=tk
MAIL_SMTP_ENCRYPTION=ssl
MAIL_SMTP_TIMEOUT=null
MAIL_SMTP_AUTH_MODE=null
[email protected]
MAIL_FROM_NAME=zongscan
生成 Mailable 可邮寄类
php bin/hyperf.php gen:mail subscribe
会生成该文件/hyperf/app/Mail/subscribe.php
<?php
declare(strict_types=1);
/**
* This file is part of hyperf-ext/mail.
*
* @link https://github.com/hyperf-ext/mail
* @contact [email protected]
* @license https://github.com/hyperf-ext/mail/blob/master/LICENSE
*/
namespace App\Mail;
use HyperfExt\Contract\ShouldQueue;
use HyperfExt\Mail\Mailable;
use App\Model\User;
class subscribe extends Mailable implements ShouldQueue
{
/**
* 用户实例。
*
* @var User
*/
public $user;
/**
* 创建一个消息实例。
*
* @param \App\Model\User $user
* @return void
*/
public function __construct(User $user)
{
//
$this->user = $user;
}
/**
* Build the message.
*/
public function build()
{
//邮箱激活模板
$html1 = <<<ht
<p>Hi,<em style="font-weight: 700;">你好 {$this->user->username}</em>,请点击下面的链接激活你的账号</p>
<a href="https://blog.zongscan.com?activate={$this->user->user_id}">立即激活</a>
ht;
return $this
->subject('ZONGSCAN-账号注册激活链接')
->htmlBody($html1);
}
}
添加路由
//发验证邮件
Router::get('sendemail/{user_id}','App\Controller\UserController@sendemail');
创建控制器方法
use App\Model\User;
use App\Mail\subscribe;
use HyperfExt\Mail\Mail;
//发邮箱验证
public function sendemail(RenderInterface $render,RequestInterface $request, int $user_id)
{
$user = User::findOrFail($user_id);
//发邮件
Mail::to('[email protected]')->send(new subscribe($user));
}
完事 看看效果
对你有帮助的话
希望你收藏本站哦:https://www.zongscan.com/
路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..博主 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 @ mashrdn 多切换几个节点测试,免费ssr是没那么稳..mashrdn 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 V2rayn免费节点添加上去了,youtobe无法打开网页,是怎么回事..张伟 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 3q!有用,不过免费节点隔天就要去git上复制新的导进去..博主 在
科学上网翻墙访问Google , 上外网神器佛跳墙VPN(永久免费)使用流程步骤中评论 该篇教程已不能用了,告知大家,免的老有老铁问我!..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号