Home > Web Front-end > JS Tutorial > Implementing nodemailer email based on Node.js_node.js

Implementing nodemailer email based on Node.js_node.js

WBOY
Release: 2016-05-16 15:17:51
Original
1679 people have browsed it

Nodemailer is a simple and easy-to-use Node.js email sending component. The specific operations are as follows

1. Install nodemailer

npm install nodemailer --save

Copy after login

2. Features

The main features of Nodemailer include:

  • Support Unicode encoding
  • Support Window system environment
  • Supports HTML content and normal text content
  • Support attachments (transfer large attachments)
  • Supports embedding images in HTML content
  • Supports SSL/STARTTLS secure email sending
  • Supports built-in transport methods and transport methods implemented by other plug-ins
  • Support custom plug-ins to process messages
  • Support XOAUTH2 login verification

The above functional features have covered most of the needs for sending emails. Next, let us start writing the program.

3. Simple example

This is a complete example to send email with clear text and HTML body

var nodemailer = require('nodemailer');

// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport('smtps://user%40gmail.com:pass@smtp.gmail.com');

// setup e-mail data with unicode symbols
var mailOptions = {
  from: 'Fred Foo &#128101; <foo@blurdybloop.com>', // sender address
  to: 'bar@blurdybloop.com, baz@blurdybloop.com', // list of receivers
  subject: 'Hello &#10004;', // Subject line
  text: 'Hello world &#128052;', // plaintext body
  html: '<b>Hello world &#128052;</b>' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
  if(error){
    return console.log(error);
  }
  console.log('Message sent: ' + info.response);
});

Copy after login

4. Common mistakes

{ [AuthError: Invalid login - 454 Authentication failed, please open smtp flag first!]
 name: 'AuthError',
 data: '454 Authentication failed, please open smtp flag first!',
 stage: 'auth' }
Copy after login

Cause of error: The account has not set up this service
Solution: QQ mailbox -> Settings -> Account -> Enable service: POP3/SMTP service

{ [SenderError: Mail from command failed - 501 mail from address must be same as authorization user]
 name: 'SenderError',
 data: '501 mail from address must be same as authorization user',
 stage: 'mail' }
Copy after login

Cause of error: The sending account and the authentication account are different, that is, the username and password do not match.

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template