邮件通知
现有邮件模板
| 模板文件 | 触发时机 |
|---|---|
welcome.hbs | 用户注册成功 |
captcha.hbs | 发送验证码 |
user-delete.hbs | 账户删除确认 |
subscription-created.hbs | 订阅创建成功 |
subscription-renewed.hbs | 订阅续费成功 |
credit.hbs | 积分购买成功 |
lifetime.hbs | 终身买断成功 |
tenant-invitation.hbs | 组织邀请 |
模板位置:api/src/config/mail/templates/
添加新邮件
1. 创建模板文件
<!-- api/src/config/mail/templates/my-notification.hbs -->
<table width="100%" cellpadding="0" cellspacing="0" style="max-width:600px; margin:20px auto; background:#ffffff; border-radius:8px; overflow:hidden; box-shadow:0 2px 10px rgba(0,0,0,0.08);">
<tr>
<td style="padding:30px 20px;">
<p style="font-size:16px; line-height:1.6; color:#333333;">Hi {{email}},</p>
<p style="font-size:16px; line-height:1.6; color:#333333;">{{message}}</p>
</td>
</tr>
</table>
模板使用 Handlebars 语法,{{变量名}} 插入数据。
2. 在业务 Service 中调用 MailService
// api/src/services/my.service.ts
import { Injectable } from "@nestjs/common"
import { MailService } from "@readystart/api-core/shared/services/mail.service"
@Injectable()
export class MyService {
constructor(private readonly mailService: MailService) {}
async sendNotification(email: string, message: string) {
const html = this.mailService.renderTemplate("my-notification", {
email,
message,
})
await this.mailService.sendMail({
to: email,
subject: "通知标题",
html,
})
}
}
不需要修改 libs/ 中的任何代码,直接在 api/src/ 业务层注入 MailService 即可。
MailService API
// 渲染模板
const html = this.mailService.renderTemplate("template-name", { key: "value" })
// 发送邮件
await this.mailService.sendMail({
to: "user@example.com",
subject: "Subject",
html: html,
})
开发模式
MAIL_SEND_ENABLED=false 时不会实际发送邮件,验证码固定为 123123。