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 ");