Home > Web Front-end > JS Tutorial > body text

Basic use of nodejs module nodemailer - sample code for sending emails that supports attachments (picture)

黄舟
Release: 2017-03-28 14:27:22
Original
2443 people have browsed it

This article mainly introduces the basic use of nodejs module nodemailer - email sending example (supports attachments). It has certain reference value. Interested friends can refer to it.

nodemailer is the email sending module in nodejs. The version used in this article is 2.5.0

--Download the module

npm install nodemailer
Copy after login

After downloading the module from npm , you can use it after introducing it in the project: var nodemailer = require('nodemailer');

Take QQ mailbox as an example.

--Get authorization code

Enter QQ personal mailbox, Settings-Account-Enable service POP3/SMTP service, and generate authorization code. Now to obtain authorization code, you need to verify your mobile phone number, etc.

Basic use of nodejs module nodemailer - sample code for sending emails that supports attachments (picture)

--Backend code

var nodemailer = require('nodemailer'); 
var transporter = nodemailer.createTransport({ 
 service: 'qq', 
 auth: { 
  user: '527828938@qq.com', 
  pass: 'ugxovfwhvxxxxxx' //授权码,通过QQ获取 
 
 } 
 }); 
 var mailOptions = { 
  from: '527828938@qq.com', // 发送者 
  to: '452076103@qq.com', // 接受者,可以同时发送多个,以逗号隔开 
  subject: 'nodemailer2.5.0邮件发送', // 标题 
  //text: 'Hello world', // 文本 
  html: `<h2>nodemailer基本使用:</h2>`  
 }; 
 
 transporter.sendMail(mailOptions, function (err, info) { 
  if (err) { 
   console.log(err); 
   return; 
  } 
 
  console.log(&#39;发送成功&#39;); 
 });
Copy after login

--Run result

##--Send attachment



var transporter = nodemailer.createTransport({ 
 service: &#39;qq&#39;, 
 auth: { 
  user: &#39;527828938@qq.com&#39;, 
  pass: &#39;ugxovfwhvxypxxxx&#39; 
 } 
 }); 
 var mailOptions = { 
  from: &#39;527828938@qq.com&#39;, // 发送者 
  to: &#39;452076103@qq.com&#39;, // 接受者,可以同时发送多个,以逗号隔开 
  subject: &#39;nodemailer2.5.0邮件发送&#39;, // 标题 
  //text: &#39;Hello world&#39;, // 文本 
  html: `<h2>nodemailer基本使用:</h2>`, 
  attachments:[ 
   { 
    filename : &#39;package.json&#39;, 
    path: &#39;./package.json&#39; 
   }, 
   { 
    filename : &#39;content&#39;, 
    content : &#39;发送内容&#39; 
   } 
  ] 
 }; 
 
 transporter.sendMail(mailOptions, function (err, info) { 
  if (err) { 
   console.log(err); 
   return; 
  } 
 
  console.log(&#39;发送成功&#39;); 
 });
Copy after login
--Run result

Download the attachment and open it

Basic use of nodejs module nodemailer - sample code for sending emails that supports attachments (picture)

This should be a good function for sending attachments.

The above is the detailed content of Basic use of nodejs module nodemailer - sample code for sending emails that supports attachments (picture). For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!