Node.js - Send email on registration

亡梦爱人 提交于 2019-12-03 06:04:41

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/8838624/node-js-send-email-on-registration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!