Hyperf2.1框架中使用hyperf-ext/mail组件包发送邮件流程步骤
swoole  /  管理员 发布于 1年前   2984
最近有个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
MAIL_SMTP_USERNAME=houtizong@163.com
MAIL_SMTP_PASSWORD=tk
MAIL_SMTP_ENCRYPTION=ssl
MAIL_SMTP_TIMEOUT=null
MAIL_SMTP_AUTH_MODE=null
MAIL_FROM_ADDRESS=houtizong@163.com
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 eric@zhu.email
* @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('houtizong@dingtalk.com')->send(new subscribe($user));
}
完事 看看效果
对你有帮助的话
希望你收藏本站哦:https://www.zongscan.com/
博主 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..xiaoB 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..BUG4 在
你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..博主 在
go语言+beego框架中获取get,post请求的所有参数中评论 @ t1 直接在router.go文件中配就ok..Jade 在
如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号