Use Node. JS to send emails

Source: Internet
Author: User
Use Node. JS to implement the mail sending function Step 1. Configuration

First, install the nodemailer library.

Npm install nodemailer // The latest version is installed by default.

For more information about this database, see nodemailer.

Step 2: Introduction to database usage

This library is easy to use. First, create an instance for sending emails.

var transporter = nodemailer.createTransport(transport[, defaults])

Transport parameter attributes

If there are too many attributes, write only some key attributes.

Port: the port number of the connection, which is generally 465.

Host: the host of the server on which you send the email. For example, 163 of the host is stmp.163.com.

Auth: This is different from the old version. The new version stores the user name and password literally. Note that stmp must be enabled for your mailbox.

User: user Name

Pass: password. If you set a third-party login password for 163, enter your third-party login password here,

You can use these settings ~ Paste my settings here

var smtpConfig = { host: 'smtp.163.com', port: 465, auth: {  user: 'xxxx',  pass: 'xxxx' }};var transporter = nodemailer.createTransport(smtpConfig);

Step 3: Usage

Now we can use the following functions to send emails

transporter.sendMail(data[, callback])

Data email content

From email sender

Recipient of to mail

Subject topic

Text emails are sent in text format.

Html mail content is html webpage Effect

Attachments attachment. For more information, see the official documentation.

Callback function

Two Parameters err and info are accepted.

Err

If it fails, you can print the object to view the relevant information.

Info

You can view a lot of sending status information.

MessageID will return the messageID value of the information.

Accepted is an array containing the address of the content received by the server.

The meaning of rejected is similar to the above, which is the opposite.

Now, we can send emails.

Var sendmail = function (html) {var option = {from: "sender", to: "accepter", subject: 'Emails from node', html: html} transporter. sendMail (option, function (error, response) {if (error) {console. log ("fail:" + error);} else {console. log ("success:" + response. messageID) ;}}) ;}sendmail ("Mail content:
This is an email sent from nodemailer ");
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.