Home > Article > Web Front-end > Share an example of steps to register an email using Node.js
Today I learned how node implements the email activation function. This function is very common. When we register an account, there will definitely be this step. Let’s take a look at how to implement this function
1. First register an email address that supports sending verification emails. NetEase’s 126 email address can be
. Log in after successful registration, then click Settings on the navigation bar and select POP3/SMTP/IMAP. Turn on the POP3/SMTP/IMAP service and set the authorization code.
2. Download the nodemailer plug-in
Enter on the command line: npm install --save nodemailer
3. Write the code to send the email:
1. Encapsulate the code to send the activation email, and then export it:
//email.js // 引入 nodemailer var nodemailer = require('nodemailer'); // 创建一个SMTP客户端配置 var config = { host: 'smtp.126.com', port: 25, auth: { user: 'xxx@126.com', //刚才注册的邮箱账号 pass: 'xxxxxx' //邮箱的授权码,不是注册时的密码 } }; // 创建一个SMTP客户端对象 var transporter = nodemailer.createTransport(config); // 发送邮件 module.exports = function (mail){ transporter.sendMail(mail, function(error, info){ if(error) { return console.log(error); } console.log('mail sent:', info.response); }); };
2. Test:
//sendtest.js var send = require('./mail-test'); // 创建一个邮件对象 var mail = { // 发件人 from: '流觞曲水 <xxx@126.com>', // 主题 subject: '测试', // 收件人 to: 'xxx@qq.com', // 邮件内容,HTML格式 text: '点击激活:xxx' //接收激活请求的链接 }; send(mail);
If successful, you can see the message sent in the test mailbox.
4. Verification steps
As far as a personal blog project I wrote before, I will briefly talk about how to implement email verification. .
1. In the database user data structure you define, there must be fields such as activation code, expiration time, and whether it has been activated, which are used to judge during activation;
{ code: String, //激活码,格式自己定义 date: Number, //过期日期,过期后不能激活 islive: Boolean //判断是否激活 }
2 . Send an activation link containing your username and activation code, like this:
// 创建一个邮件对象 var mail = { // 发件人 from: '小静博客 <xiaojing@126.com>', // 主题 subject: '激活账号', // 收件人 to: usermess.email, //发送给注册时填写的邮箱 // 邮件内容,HTML格式 text: '点击激活:<a href="http://localhost:3000/checkCode?name='+ usermess.name +'&code='+ usermess.code + '" rel="external nofollow" ></a>' };
3 . Respond to the activation request , search based on the user name of the activation link. If the user exists, determine whether the activation code is consistent, and determine whether the activation code has expired. If all are correct, change the activation status. At this time, the activation is successful, as follows:
exports.checkCode = function (req, res){ var username = req.query.name; var code = req.query.code; var outdate = req.query.outdate; User.findOne({name: username}, function (err, user){ if (user.code === code && (user.date - Date.now()) > 0){ User.update({name: username}, {islive: true}, function (err){ if (err){ res.render('login', { title: '登录', error: '激活失败!' }); }else{ res.render('login', { title: '登录', error: '激活成功请登录!' }); } }); } }); }
The above is the editor’s introduction to how Node.js implements the registration email activation function. I hope it will be helpful to everyone. If you have any questions, please leave me a message. Editor Will reply to everyone in time!
The above is the detailed content of Share an example of steps to register an email using Node.js. For more information, please follow other related articles on the PHP Chinese website!