Node.js - 在注册时发送电子邮件 [英] Node.js - Send email on registration
问题描述
我看过这个具有注册表单的Node.js示例。但是它只发送欢迎功能。
有没有发送注册邮件的Node.js应用程序的示例?
到目前为止我还没有看到这样一个例子,但你的第二个问题是什么?您提供的示例显示了如何发送电子邮件。另一个选择是使用这个软件包:
https: /github.com/Marak/node_mailer
哪些在发送电子邮件方面似乎也有很好的记录。
所以我假设你想知道如何设置注册系统。执行此操作的一种方法是具有用于注册具有电子邮件
和令牌
列的用户的表。 电子邮件
是明显的,令牌
是一个随机生成的字符串(例如,使用节点的加密.randomBytes
方法),它将作为链接的一部分发送给用户。进入链接后,您在数据库中搜索该令牌,如果找到,则继续注册。
需要注意的两件事情:创建令牌时,请确保它已经不存在于db中。第二:使用 valid_until
列删除超过几个小时的令牌是一个很好的做法。
更新:
不幸的是,节点的base64导出不是url-safe。因此,这是获取我发现的安全令牌的最简单的方法:
require('crypto')。randomBytes 48,function(ex,buf){
token = buf.toString('base64')。replace(/ \ // g,'_')。replace(/ \ + / g,' - ' ));
});
I have signup form with the single email field. When an user enters its email I need to send a registration link.
I've seen this Node.js example with signup form. But it has sendWelcome feature only.
Are there any examples of Node.js apps with sending registration email?
I haven't seen such an example so far, but what is your secondary question? The example you've provided shows pretty well how to send an e-mail. Another option is to use this package:
https://github.com/Marak/node_mailer
Which also seems to be well documented on how to send e-mails.
Therefore I assume that you'd like to know how to setup the sign-up system. One way to do this is to have a table for registering users which has e-mail
and token
columns. E-mail
is obvious, token
is a randomly generated string (for example with node's crypto.randomBytes
method) that will be send as a part of the link to the user. Upon entering the link, you search the database for this token and if it's found, you proceed with the registration.
Two things to note: when creating the token, make sure that it doesn't exist in the db already. Second: it's a good practice to use a valid_until
column to remove tokens older than several hours.
Update:
Unfortunately, node's base64 export is not url-safe. Therefore, this is the easiest method to obtain the secure token I've found:
require('crypto').randomBytes(48, function(ex, buf) {
token = buf.toString('base64').replace(/\//g,'_').replace(/\+/g,'-'));
});
Perhaps someone will come up with a better solution.
这篇关于Node.js - 在注册时发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!