乐闻世界logo
第 2 / 24 篇

基于Nestjs实现node发送邮件

第 2 / 24 篇
1 零基础入门 Nestjs 开发教程2基于Nestjs实现node发送邮件3一文教会你如何使用 Vercel 部署你的 NestJS 应用 4NestJS 如何进行服务端推送SSE、自定义服务端推送内容5如何在 NestJS 中集成 MongoDB 并实现数据的增删改查操作6如何在 NestJS 中集成 Redis 并基于 Redis 实现接口访问限频率7基于 NestJS 集成 ElasticSearch 实现模糊搜索功能8如何在 NestJS 项目中优雅的使用发布订阅工具 Event Emitter9一篇文章学会使用 NestJS 的 Module 实现高效的代码模块管理10一篇文章学会如何使用 NestJS 的五种 Provider 提供者11 一篇文章学会如何使用 NestJS 的 管道Pipes 实现高效的数据转换和验证12基于 NestJS 操作 TypeORM 中的多对多 ManyToMany13基于 NestJS 操作 TypeORM 中的一对多 OneToMany14一篇文章学会如何使用 NestJS 的 Guards 守卫实现系统身份验证和授权15一篇文章学会 NestJS 中间件的使用,让你的应用更灵活和可扩展16一篇文章学会如何使用 NestJS 过滤器处理全局异常情况17一篇文章学会 NestJS 的拦截器并且附带实战操作案例18NestJS 如何实现接口多版本控制19如何在 NestJS 中安全高效的管理 Config 配置20一篇文章掌握 NestJS 所有的生命周期以及执行时机21如何在 Nest 项目中自定义装饰器22NestJS 基于 Passort 和 JWT Token 实现接口的权限管理23基于NestJS 和 TypeORM 实现 CURD RESTful API接口24探索 @nestjs/cqrs:在 NestJS 中实现命令查询责任分离模式CQRS
返回教程主页

基于Nestjs实现node发送邮件

背景效果落地方案依赖安装npminstall@nestjs-modules/mailer初始化module//app.module.ts@Module({imports:[MailerModule.forRoot({transport:{host:'smtp.exmail.qq.com',port:465,ignoreTLS:false,secure:true,auth:{user:'bot@fm

背景

效果

落地方案

  1. 依赖安装

    shell
    npm install @nestjs-modules/mailer
  2. 初始化 module

    javascript
    // app.module.ts @Module({ imports: [ MailerModule.forRoot({ transport: { host: 'smtp.exmail.qq.com', port: 465, ignoreTLS: false, secure: true, auth: { user: 'bot@fmlg1688.cn', pass: 'xxxxxx', }, }, defaults: { from: '"Bot" <bot@fmlg1688.cn>', }, preview:false, template: { dir: path.join(process.cwd(), './src/common/template'),//这里就是你的ejs模板文件夹路径 adapter: new EjsAdapter(), options: { strict: true //严格模式 } } }) ], ... }) export class AppModule { }
  3. 邮件模版

    javascript
    // common/template/validate.ejs <div class="biu-nav-email" style="max-width: 600px;min-width: 300px;margin: 40px auto;box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1);font-size: 16px;border-radius: 5px;color: #ccc;"> <h3 style="margin-bottom: 40px;"> Hi! 亲爱的用户: </h3> <p style="padding-left: 20px;"> 您正在进行邮箱验证,本次请求的验证码为: </p> <p style="color: #dde2e2;padding-left: 14px;"> <strong style="color: #000;font-size: 24px;"> <%= locals.code %> </strong> <span>(为了保障您帐号的安全性,请在30分钟内完成验证,祝您生活愉快!)</span> </p> <p style="padding-left: 20px;"> <span>快速访问:</span> <a href="https://www.biunav.com" style="color:#fff" target="_blank" rel="noopener noreferrer">https://www.biunav.com</a> </p> </div>
  4. 发送邮件

    javascript
    import { MailerService } from '@nestjs-modules/mailer'; import { Controller, Get } from '@nestjs/common'; @Controller({ path: '/mail' }) export class MailController { constructor(private readonly mailerService: MailerService) {} @Get() async classify() { let result = await this.mailerService .sendMail({ to: '1025534801@qq.com', from: 'bot@fmlg1688.cn', subject: '测试', template: './validate.ejs', context: { code: 3311, }, }) .then((result) => { console.log('---->'); }) .catch((error) => { console.log('error:', error); }); return { code: 0, result }; } }