发送电子邮件是许多应用程序中的一项关键功能,无论是用于用户通知、事务更新还是营销目的。然而,实施电子邮件解决方案有时可能很麻烦,因为您必须将邮件程序与模板语言集成,检查依赖关系......
但是!
使用 @nestixis/nestjs-mailer 包,您可以简化此过程,同时确保灵活性和可靠性。
这个包利用了 React 和 Nodemailer 的强大功能,使其成为一个现代且对开发人员友好的工具,可以轻松构建动态电子邮件模板并发送电子邮件。
让我们深入了解如何设置和使用它:)
安装包
首先,您需要在 NestJS 应用程序中安装 Nestjs-mailer 包。该软件包可通过 npm 获得,使安装快速而简单。在终端中运行以下命令:
npm install @nestixis/nestjs-mailer
配置模块
安装软件包后,下一步是在您的应用程序中配置 MailerSdkModule。
配置很简单,出于测试目的,您可以使用 Mailcatch 等工具来捕获和预览电子邮件,而无需将其发送给真实用户。以下是如何设置的示例:
import { MailerSdkModule } from '@nestixis/nestjs-mailer'; import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; @Module({ imports: [ MailerSdkModule.register({ auth: { user: 'username', password: 'password', host: 'sandbox-smtp.mailcatch.app', port: 2525, ssl: false, }, from: 'your-email@example.com', }), ], controllers: [AppController], providers: [AppService], }) export class AppModule {}
创建电子邮件模板
为了使您的电子邮件在视觉上更有吸引力且更具活力,您可以将模板与 React 结合使用,而 package@react-email/components 允许您设计此类电子邮件模板。
但在此之前,您应该调用文件 inform-admin-with-account-template.tsx 并设置
“jsx”:“反应”
在你的 tsconfig.json
以下是邀请新管理员用户的模板示例:
import { Body, Container, Head, Html, Img, Link, Section, Text, } from '@react-email/components'; import * as React from 'react'; export default function InviteAdminWithAccountTemplate({ translation, language, invitationHref, passwordHref, logoUrl, }) { return ( <style>{/* Your custom styles here */}</style> <hr> <h2> Injecting the Email Sender </h2> <p>After creating your email template, the next step is to send the email. To do this, you inject the email sender into your service.<br> </p> <pre class="brush:php;toolbar:false">import { EmailSenderInterface, MAILER_SDK_CLIENT, } from '@nestixis/nestjs-mailer'; import { Inject, Injectable } from '@nestjs/common'; import InviteAdminWithAccountTemplate from './invite-admin-with-account-template'; @Injectable() export class AppService { constructor( @Inject(MAILER_SDK_CLIENT) private readonly emailSender: EmailSenderInterface, ) {} async send(): Promise<void> { const translations = { titleInside: { subpart1: 'Welcome', subpart2: ' to the platform!' }, contentPart1: 'Hello', contentPart2: 'Your admin account has been created.', contentPart3: { subpart1: 'Click here to activate your account: ', subpart2: 'Activate', subpart3: '.', }, contentPart4: { subpart1: 'To set your password, click here: ', subpart2: 'Set password', }, }; const emailContent = await InviteAdminWithAccountTemplate({ translation: translations, language: 'en', invitationHref: 'xxx', passwordHref: 'xxx', logoUrl: 'logo.png', }); await this.emailSender.sendEmail( 'test@test.com', 'Admin Invitation', emailContent, ); } } </void>
完毕!
就是这样!您已成功将 Nestjs-mailer 集成到您的应用程序中。
有关更多详细信息和高级功能,请查看 NestJS 邮件程序 GitHub 存储库。
以上是如何在NestJS中轻松发送电子邮件?的详细内容。更多信息请关注PHP中文网其他相关文章!

JavaScript核心数据类型在浏览器和Node.js中一致,但处理方式和额外类型有所不同。1)全局对象在浏览器中为window,在Node.js中为global。2)Node.js独有Buffer对象,用于处理二进制数据。3)性能和时间处理在两者间也有差异,需根据环境调整代码。

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python和JavaScript的主要区别在于类型系统和应用场景。1.Python使用动态类型,适合科学计算和数据分析。2.JavaScript采用弱类型,广泛用于前端和全栈开发。两者在异步编程和性能优化上各有优势,选择时应根据项目需求决定。

选择Python还是JavaScript取决于项目类型:1)数据科学和自动化任务选择Python;2)前端和全栈开发选择JavaScript。Python因其在数据处理和自动化方面的强大库而备受青睐,而JavaScript则因其在网页交互和全栈开发中的优势而不可或缺。

Python和JavaScript各有优势,选择取决于项目需求和个人偏好。1.Python易学,语法简洁,适用于数据科学和后端开发,但执行速度较慢。2.JavaScript在前端开发中无处不在,异步编程能力强,Node.js使其适用于全栈开发,但语法可能复杂且易出错。

javascriptisnotbuiltoncorc; saninterpretedlanguagethatrunsonenginesoftenwritteninc.1)javascriptwasdesignedAsalightweight,解释edganguageforwebbrowsers.2)Enginesevolvedfromsimpleterterterpretpreterterterpretertestojitcompilerers,典型地提示。

JavaScript可用于前端和后端开发。前端通过DOM操作增强用户体验,后端通过Node.js处理服务器任务。1.前端示例:改变网页文本内容。2.后端示例:创建Node.js服务器。

选择Python还是JavaScript应基于职业发展、学习曲线和生态系统:1)职业发展:Python适合数据科学和后端开发,JavaScript适合前端和全栈开发。2)学习曲线:Python语法简洁,适合初学者;JavaScript语法灵活。3)生态系统:Python有丰富的科学计算库,JavaScript有强大的前端框架。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 Linux新版
SublimeText3 Linux最新版

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。