search
HomeWeb Front-endJS TutorialHow to use Node.js to register for email activation

This time I will show you how to use Node.js to register for email activation, and what are the precautions for using Node.js to register for email activation. The following is a practical case, let’s take a look.

1. Register an email address

First register an email address that supports sending verification emails. The one I registered here is NetEase’s 163 email address, so the 163 email address is used below. After successful registration, log in to the sending email address

, then click Settings on the navigation bar, select POP3/SMTP/IMAP, enable 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

3.1 Encapsulate the code for sending the activation email, and then export it:

//email.js
// 引入 nodemailer
const nodemailer = require('nodemailer');
// 创建一个SMTP客户端配置
const config = {
    host: 'smtp.163.com',
    port: 465,
    auth: {
      user: 'xxxx@163.com', //刚才注册的邮箱账号
      pass: 'xxxxxx' //邮箱的授权码,不是注册时的密码
    }
  };
// 创建一个SMTP客户端对象
const 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);
  });
};

3.2 Test:

//sendtest.js
var send = require('./email.js');
// 创建一个邮件对象
var mail = {
  // 发件人
  from: '极客教程 <xxxx>',
  // 主题
  subject: '[极客教程]激活邮箱账号',
  // 收件人
  to: 'xxxx@qq.com',
  // 邮件内容,HTML格式
  text: `尊敬的${user.name},您好!点击链接即可激活您的极客教程
      网账号,http://localhost:3000/checkCode?name=${user.name}&code=${user.code}为保障您的帐号安全,请在24小时内点击该链接,您也可以将链接复制到浏览器地址栏访问。 若如果您并未尝试修改密码,请忽略本邮件,由此给您带来的不便请谅解。本邮件由系统自动发出,请勿直接回复!` //接收激活请求的链接
};
send(mail);</xxxx>

If successful, you can see the email sent in the test mailbox information.

4. Verification steps

Let’s briefly talk about how to implement email verification.

1. In the database user data structure defined by yourself, 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 Activation link, which contains user name and activation code, as follows:

// 创建一个邮件对象
var mail = {
  // 发件人
  from: '极客教程 <xxxx>',
  // 主题
  subject: '[极客教程]激活邮箱账号',
  // 收件人
  to: 'xxxx@qq.com',
  // 邮件内容,HTML格式
  text: `尊敬的${user.name},您好!点击链接即可激活您的极客教程
      网账号,http://localhost:3000/checkCode?name=${user.name}&code=${user.code}为保障您的帐号安全,请在24小时内点击该链接,您也可以将链接复制到浏览器地址栏访问。 若如果您并未尝试修改密码,请忽略本邮件,由此给您带来的不便请谅解。本邮件由系统自动发出,请勿直接回复!` //接收激活请求的链接
};
send(mail);</xxxx>

3. Respond to the activation request and search according to the user name of the activation link. If the user exists, determine whether the activation code is consistent and determine whether the activation code is consistent. Whether it has expired, and if everything is correct, the activation status will be changed. At this time, the activation is successful, as shown in the following code:

// check email code
exports.checkCode = function (req, res){
  var username = req.query.name;
  var code = req.query.code;
  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.json({error: true})
              }else{
                  console.log(user)
                  res.json({ok: true})
              }
          });
      }else{
        res.json({
          email: user.mail,
          failure: true
        })
      }
  });
}

5. Problems encountered

The following problems were encountered during development:

{ [AuthError: Invalid login - 535 Error: authentication failed]
name: 'AuthError',
data: '535 Error: authentication failed',
stage: ' auth' }

smtp server verification failed because NetEase's email has authorization restrictions. Be sure to check the account number and authorization code when you registered your email.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the mutual communication function of controllers in Angularjs

node makes a personalized command line tool

The above is the detailed content of How to use Node.js to register for email activation. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Vercel是什么?怎么部署Node服务?Vercel是什么?怎么部署Node服务?May 07, 2022 pm 09:34 PM

Vercel是什么?本篇文章带大家了解一下Vercel,并介绍一下在Vercel中部署 Node 服务的方法,希望对大家有所帮助!

node.js gm是什么node.js gm是什么Jul 12, 2022 pm 06:28 PM

gm是基于node.js的图片处理插件,它封装了图片处理工具GraphicsMagick(GM)和ImageMagick(IM),可使用spawn的方式调用。gm插件不是node默认安装的,需执行“npm install gm -S”进行安装才可使用。

怎么使用pkg将Node.js项目打包为可执行文件?怎么使用pkg将Node.js项目打包为可执行文件?Jul 26, 2022 pm 07:33 PM

如何用pkg打包nodejs可执行文件?下面本篇文章给大家介绍一下使用pkg将Node.js项目打包为可执行文件的方法,希望对大家有所帮助!

一文解析package.json和package-lock.json一文解析package.json和package-lock.jsonSep 01, 2022 pm 08:02 PM

本篇文章带大家详解package.json和package-lock.json文件,希望对大家有所帮助!

分享一个Nodejs web框架:Fastify分享一个Nodejs web框架:FastifyAug 04, 2022 pm 09:23 PM

本篇文章给大家分享一个Nodejs web框架:Fastify,简单介绍一下Fastify支持的特性、Fastify支持的插件以及Fastify的使用方法,希望对大家有所帮助!

node爬取数据实例:聊聊怎么抓取小说章节node爬取数据实例:聊聊怎么抓取小说章节May 02, 2022 am 10:00 AM

node怎么爬取数据?下面本篇文章给大家分享一个node爬虫实例,聊聊利用node抓取小说章节的方法,希望对大家有所帮助!

手把手带你使用Node.js和adb开发一个手机备份小工具手把手带你使用Node.js和adb开发一个手机备份小工具Apr 14, 2022 pm 09:06 PM

本篇文章给大家分享一个Node实战,介绍一下使用Node.js和adb怎么开发一个手机备份小工具,希望对大家有所帮助!

图文详解node.js如何构建web服务器图文详解node.js如何构建web服务器Aug 08, 2022 am 10:27 AM

先介绍node.js的安装,再介绍使用node.js构建一个简单的web服务器,最后通过一个简单的示例,演示网页与服务器之间的数据交互的实现。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft