I’ve been working on a lot of server modules with Nodejs, so I’m going to show you how to use Nodejs to send emails to users.

The author will introduce the implementation scheme of automatic email sending in detail, and guide you to master using NodeJS automatic email sending through a practical case. Finally, some practical application scenarios will be introduced to deepen the understanding of this scheme and achieve the purpose of applying what you have learned.

Implementation scheme

Nodemailer is used to send email automatically. Nodemailer can send email easily. Nodemailer can send email automatically.

Nodemailer was chosen because it provides very flexible custom configurations and security guarantees, such as:

  • A single module with zero dependencies, easy to audit code, no dead ends
  • UnicodeAny character is supported, including emoticons 💪
  • Email content supports both plain text and customizationhtml
  • Supports custom attachments
  • Supports safe and reliableSSL/STARTTLSMail delivery
  • Support for custom plug-ins to handle mail messages

There are many characteristics of the author will not be introduced. Let’s take a look at a simplified and translated case on the official website:

"use strict";
const nodemailer = require("nodemailer");

/ / use the async.. Await creates an execution function
async function main() {
  // You can use this method to create a test mailbox if you do not have a real mailbox
  let testAccount = await nodemailer.createTestAccount();

  // Create Nodemailer transporter SMTP or other transport mechanism
  let transporter = nodemailer.createTransport({
    host: "smtp.ethereal.email".// The host address of the third-party mailbox
    port: 587.secure: false.// true for 465, false for other ports
    auth: {
      user: testAccount.user, // The sender's email account
      pass: testAccount.pass, // Email authorization password}});// Define the transport object and send the mail
  let info = await transporter.sendMail({
    from: '" Dooring 👻 "< [email protected] >'.// The sender's email account
    to: "[email protected], [email protected]".// Email recipient's account
    subject: "Hello Dooring".// Subject line
    text: "H5-Dooring?".// Text content
    html: ${emailCode}.// HTML content. If HTML content is set, text content is ignored
  });
}

main().catch(console.error);
Copy the code

The code above is a complete example of sending an email with plain text and HTML body. The author has done a detailed translation in the code, and we can conclude that in order to send an email, we need the following three steps:

  • createNodemailerThe transmitterSMTPOr some other transportation mechanism
  • Set up theMessageOptions (what message to send to whom)
  • Using the previously created transportThe sendMail ()Method passes a message object

We know the macro process of use, and then we implement each technical function point to achieve. We need to focus on the following core points:

  • How to set uphost
  • How to set upauth
  • How to configureMessageoptions

With these three questions answered, we have the flexibility to use Nodemailer to send custom mail.

How to set host, port, secure

Here I take netease mailbox as an example. For example, if we want to use our own netease mailbox to send emails to users, we need to register a netease mailbox for sending emails, such as [email protected]. Since we are using an SMTP transport, on the mailbox home page we find the following options and set them:

The corresponding host can be found at the bottom of the page as follows:

As for port and Secure, we can use the default Settings. If secure is set to true, 465 is used by default. The detailed configuration is as follows:

How do I set auth

Auth we have already mentioned in the previous step. When we start IMAP/SMTP service, we will prompt you to save the email authorization code. In this case, the authorization code is the value of Auth. pass, and auth.user is the currently authorized email.

How to set Message

Message configuration is an important part of our mailbox service. The following configuration instructions are provided officially:

Here I give you a detailed introduction:

  • From Email address of the sender. All email addresses can be plain ‘[email protected] ‘or formatted ‘sender name ‘[email protected]
  • To comma-separated list or arrangement of recipients’ E-mail addresses
  • A CC comma-separated list or array of recipient E-mail addresses will be displayed in the CC field
  • A BCC comma-separated list or array of recipient E-mail addresses will be displayed in the BCC: field
  • Subject Indicates the subject of an email
  • Text Indicates the text content of a message
  • HTML The HTML content of the message. If HTML is defined, text is ignored
  • Attachments

After being familiar with the above configuration, we can configure the email sending requirements for 80% scenarios. Here’s a simple example of setting Message:

await transporter.sendMail({
  from: '"v6.dooring" <[email protected]>'.// sender address
  to: '[email protected]'.// list of receivers
  subject: 'welcome to use dooring'.// Subject line
  text: 'Hello world? '.// plain text body
  html: 'Welcome to register v6.dooring, your mailbox verification code is :<b>${emailCode}</b>`.// html body
})
Copy the code

This example is a scenario in which the Dooring mailbox is used to send a mailbox verification code to an ordinary user. This scenario is currently used in many login and registration related scenarios. Let’s look at this in action:

extension

Nodemailer can be used to develop many interesting products, such as:

  • Online email client
  • Customize the mailbox template
  • Mailbox authentication central system
  • Email group assistant

For the second application scenario, there are many beautiful open source email templates, such as

  • Foundation for Emails
  • emailframe emailframe.work
  • MJML mjml.io

The last

At present, I am also constantly updating the H5 editor H5-Dooring. Recent updates are as follows:

  • Add provincial and municipal joint components
  • Added the ability to import Excel data in batches
  • Add form custom validation
  • Add configuration items such as automatic play control and loop play for audio components
  • Add a horizontal sliding component
  • Added file upload functionality to form Designer
  • Added big wheel raffle component
  • Added a 9 grid raffle component
  • Added component permission control
  • Added the custom upload function of photo library

Think it works? Like to collect, by the way point like it, your support is my biggest encouragement! Wechat search “interesting talk front-end”, found more interesting H5 games, Webpack, Node, gulp, CSS3, javascript, nodeJS, Canvas data visualization and other front-end knowledge and actual combat.