node.js - 发送电子邮件的最正确方式是什么?
问题描述
我正在学习 TypeOrm,我正在尝试在用户创建帐户后实现电子邮件验证系统。
假设我有两个实体,用户和电子邮件验证。创建用户时,将在数据库中插入与该用户相关的 EmailVerification。下一步是在创建 EmailVerification 后立即向该用户发送电子邮件。
但我不确定使用什么 typeOrm 功能来调用我的电子邮件服务发送功能。
我在想两种方法来实现这一点,
1 - 在数据库中插入用户和电子邮件验证后,作为补充步骤的交易:
await getManager().transaction(async entityManager => {
await entityManager.save(user);
await entityManager.save(emailVerification);
// send the message directly from the transaction right after the user and emailVerification is created
await emailService.send(message);
})
2 - 在创建 EmailEntity 后立即从 EntitySubscriber :
@EventSubscriber()
export class EmailVerificationSubscriber implements EntitySubscriberInterface<EmailVerification> {
@AfterInsert()
sendEmail() {
// ... //
// get related user email
// ... //
// then send the message
await emailService.send(message);
}
}
这两种方法对我来说似乎足够了,但我想知道这个用例是否有某种最佳实践?
如果需要,我可以提供更多信息
解决方案
您选择哪个选项并不重要。
话虽如此,您发送的电子邮件是您插入emailVerification
对象时开始的工作流程的一部分。因此,将其与该操作相关联是有意义的。
如果您将来使用类似的工作流程进行密码恢复,那么为什么这样做是有道理的就很明显了。
推荐阅读
- jquery - 更改在页面中多个位置重复的按钮的状态,但仅更改被单击的按钮,而不更改其余按钮
- angularjs - 在检查元素的浏览器源代码中隐藏使用 ng-show 注释的 HTML 元素
- google-analytics-api - 当范围在 2016-08-23 之前开始时,GA 报告 API 返回 0 ga:users
- graphql - CraftQL - 无法在矩阵字段中找到标签字段
- node.js - 如何将 req.params 传递/使用到前端 js
- javascript - 这两个不同的代码如何返回相同的结果
- php - 使用 SimpleXml 从 xml 文件中提取数据
- c++ - PCL:如何从 PCL 1.8.1 中的 k-means 聚类中提取聚类标签?
- json - 如果多个键具有指定值,则使用 jq 返回布尔值
- django - Django 超级用户创建无响应