Node.js - 在注册时发送电子邮件 [英] Node.js - Send email on registration

查看:152
本文介绍了Node.js - 在注册时发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电子邮件字段的注册表单。当用户输入电子邮件时,我需要发送注册链接。



我看过这个具有注册表单的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆