NestJS Mailer 模块使用教程
1. 项目介绍
NestJS Mailer 是一个用于 NestJS 框架的邮件发送模块。它基于 Nodemailer,提供了简单易用的接口来发送电子邮件。该模块支持多种邮件传输方式,包括 SMTP、SendGrid、Mailgun 等,并且可以轻松集成到 NestJS 应用中。
2. 项目快速启动
2.1 安装依赖
首先,你需要在你的 NestJS 项目中安装 @nestjs-modules/mailer 和 @nestjs/config 包:
npm install @nestjs-modules/mailer nodemailer
npm install @nestjs/config
2.2 配置邮件服务
在 src/app.module.ts 文件中配置邮件服务:
import { Module } from '@nestjs/common';
import { MailerModule } from '@nestjs-modules/mailer';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
ConfigModule.forRoot(),
MailerModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (config: ConfigService) => ({
transport: {
host: config.get('MAIL_HOST'),
port: config.get('MAIL_PORT'),
secure: false,
auth: {
user: config.get('MAIL_USER'),
pass: config.get('MAIL_PASSWORD'),
},
},
defaults: {
from: `"No Reply" <${config.get('MAIL_FROM')}>`,
},
}),
inject: [ConfigService],
}),
],
})
export class AppModule {}
2.3 发送邮件
创建一个邮件服务类 src/mail/mail.service.ts:
import { Injectable } from '@nestjs/common';
import { MailerService } from '@nestjs-modules/mailer';
@Injectable()
export class MailService {
constructor(private mailerService: MailerService) {}
async sendWelcomeEmail(to: string) {
await this.mailerService.sendMail({
to,
subject: 'Welcome to our service!',
template: 'welcome', // 使用模板引擎
context: {
name: 'John Doe',
},
});
}
}
2.4 配置模板引擎
在 src/mail/templates 目录下创建一个 welcome.hbs 模板文件:
<h1>Welcome, {{name}}!</h1>
<p>Thank you for joining our service.</p>
2.5 使用邮件服务
在控制器或服务中使用邮件服务:
import { Controller, Post, Body } from '@nestjs/common';
import { MailService } from './mail/mail.service';
@Controller('mail')
export class MailController {
constructor(private mailService: MailService) {}
@Post('send')
async sendEmail(@Body('email') email: string) {
await this.mailService.sendWelcomeEmail(email);
return { message: 'Email sent' };
}
}
3. 应用案例和最佳实践
3.1 用户注册欢迎邮件
在用户注册成功后,发送一封欢迎邮件。可以使用模板引擎来动态生成邮件内容,例如用户的名字。
3.2 密码重置邮件
当用户请求重置密码时,发送一封包含重置链接的邮件。确保链接的安全性,并在邮件中包含过期时间。
3.3 订单确认邮件
在用户下单后,发送一封订单确认邮件,包含订单详情和预计送达时间。
4. 典型生态项目
4.1 NestJS
NestJS 是一个用于构建高效、可扩展的 Node.js 服务器端应用程序的框架。NestJS Mailer 模块是 NestJS 生态系统的一部分,提供了强大的邮件发送功能。
4.2 Nodemailer
Nodemailer 是一个用于发送电子邮件的 Node.js 模块。NestJS Mailer 模块基于 Nodemailer,提供了更高层次的抽象,方便在 NestJS 应用中使用。
4.3 Handlebars
Handlebars 是一个简单的模板引擎,可以用于生成动态的 HTML 内容。NestJS Mailer 支持使用 Handlebars 模板引擎来生成邮件内容。
通过以上步骤,你可以快速上手并使用 NestJS Mailer 模块来发送电子邮件。希望这篇教程对你有所帮助!
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00
GLM-4.7-FlashGLM-4.7-Flash 是一款 30B-A3B MoE 模型。作为 30B 级别中的佼佼者,GLM-4.7-Flash 为追求性能与效率平衡的轻量化部署提供了全新选择。Jinja00
VLOOKVLOOK™ 是优雅好用的 Typora/Markdown 主题包和增强插件。 VLOOK™ is an elegant and practical THEME PACKAGE × ENHANCEMENT PLUGIN for Typora/Markdown.Less00
PaddleOCR-VL-1.5PaddleOCR-VL-1.5 是 PaddleOCR-VL 的新一代进阶模型,在 OmniDocBench v1.5 上实现了 94.5% 的全新 state-of-the-art 准确率。 为了严格评估模型在真实物理畸变下的鲁棒性——包括扫描伪影、倾斜、扭曲、屏幕拍摄和光照变化——我们提出了 Real5-OmniDocBench 基准测试集。实验结果表明,该增强模型在新构建的基准测试集上达到了 SOTA 性能。此外,我们通过整合印章识别和文本检测识别(text spotting)任务扩展了模型的能力,同时保持 0.9B 的超紧凑 VLM 规模,具备高效率特性。Python00
KuiklyUI基于KMP技术的高性能、全平台开发框架,具备统一代码库、极致易用性和动态灵活性。 Provide a high-performance, full-platform development framework with unified codebase, ultimate ease of use, and dynamic flexibility. 注意:本仓库为Github仓库镜像,PR或Issue请移步至Github发起,感谢支持!Kotlin07
compass-metrics-modelMetrics model project for the OSS CompassPython00