原文连接:Node.js使用Nodemailer发送邮件javascript
电子邮件是—种用电子手段提供信息交换的通讯方式,是互联网应用最广的服务。经过网络的电子邮件系统,用户能够以很是低廉的价格(无论发送到哪里,都只需负担网费)、很是快速的方式(几秒钟以内能够发送到世界上任何指定的目的地),与世界上任何一个角落的网络用户联系。html
在不少项目中,咱们都会遇到邮件注册,邮件反馈等需求。在node中收发电子邮件也很是简单,由于强大的社区有各类各样的包能够供我么直接使用。Nodemailer包就能够帮助咱们快速实现发送邮件的功能。java
Github源码:https://github.com/ogilhinn/node-abc/tree/master/lesson10node
Nodemailer是一个简单易用的Node.js邮件发送组件git
官网地址:https://nodemailer.comgithub
GitHub地址:https://github.com/nodemailer/nodemailernpm
Nodemailer的主要特色包括:安全
首先,咱们确定是要下载安装 注意:Node.js v6+bash
npm install nodemailer --save
打开官网能够看见一个小例子网络
'use strict'; const nodemailer = require('nodemailer'); // Generate test SMTP service account from ethereal.email // Only needed if you don't have a real mail account for testing nodemailer.createTestAccount((err, account) => { // create reusable transporter object using the default SMTP transport let transporter = nodemailer.createTransport({ host: 'smtp.ethereal.email', port: 587, secure: false, // true for 465, false for other ports auth: { user: account.user, // generated ethereal user pass: account.pass // generated ethereal password } }); // setup email data with unicode symbols let mailOptions = { from: '"Fred Foo ?" <foo@blurdybloop.com>', // sender address to: 'bar@blurdybloop.com, baz@blurdybloop.com', // list of receivers subject: 'Hello ✔', // Subject line text: 'Hello world?', // plain text body html: '<b>Hello world?</b>' // html body }; // send mail with defined transport object transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log('Message sent: %s', info.messageId); // Preview only available when sending through an Ethereal account console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info)); // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@blurdybloop.com> // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou... }); });
这个小例子是生成了Ethereal的帐户进行邮件发送演示的。可是这多没意思,咱们来使用本身的邮箱来发送邮件
这里我使用了个人qq邮箱给163邮箱发送email。
'use strict'; const nodemailer = require('nodemailer'); let transporter = nodemailer.createTransport({ // host: 'smtp.ethereal.email', service: 'qq', // 使用了内置传输发送邮件 查看支持列表:https://nodemailer.com/smtp/well-known/ port: 465, // SMTP 端口 secureConnection: true, // 使用了 SSL auth: { user: 'xxxxxx@qq.com', // 这里密码不是qq密码,是你设置的smtp受权码 pass: 'xxxxxx', } }); let mailOptions = { from: '"JavaScript之禅" <xxxxx@qq.com>', // sender address to: 'xxxxxxxx@163.com', // list of receivers subject: 'Hello', // Subject line // 发送text或者html格式 // text: 'Hello world?', // plain text body html: '<b>Hello world?</b>' // html body }; // send mail with defined transport object transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log('Message sent: %s', info.messageId); // Message sent: <04ec7731-cc68-1ef6-303c-61b0f796b78f@qq.com> });
运行程序,成功将返回messageId。这是即可以去收件箱查看这个新邮件啦
这里咱们须要注意,auth.pass 不是邮箱帐户的密码而是stmp的受权码。
到此咱们就掌握了发邮件的基本操做。
更多配置项:https://nodemailer.com/message/
这里咱们就不演示CC、BCC了,请自行尝试。咱们来试试发送附件
... // 只需添加attachments配置项便可 attachments: [ { // utf-8 string as an attachment filename: 'text.txt', content: 'hello world!' }, { filename: 'ZenQcode.png', path: path.resolve(__dirname, 'ZenQcode.png'), } ] ...
发送email,就能够收到一个内容为hello world的text.txt文件,以及一个我公众号的二维码。
HTML Email 编写指南: http://www.ruanyifeng.com/blog/2013/06/html_email.html
这儿,咱们使用Foundation for Emails: https://foundation.zurb.com/emails.html的模板
'use strict'; const nodemailer = require('nodemailer'); const ejs = require('ejs'); const fs = require('fs'); const path = require('path'); let transporter = nodemailer.createTransport({ // host: 'smtp.ethereal.email', service: 'qq', // 使用内置传输发送邮件 查看支持列表:https://nodemailer.com/smtp/well-known/ port: 465, // SMTP 端口 secureConnection: true, // 使用 SSL auth: { user: 'xxxxxx@qq.com', // 这里密码不是qq密码,是你设置的smtp受权码 pass: 'xxxxxx', } }); let mailOptions = { from: '"JavaScript之禅" <xxxxx@qq.com>', // sender address to: 'xxxxxxxx@163.com', // list of receivers subject: 'Hello', // Subject line // 发送text或者html格式 // text: 'Hello world?', // plain text body html: fs.createReadStream(path.resolve(__dirname, 'email.html')) // 流 }; // send mail with defined transport object transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log('Message sent: %s', info.messageId); // Message sent: <04ec7731-cc68-1ef6-303c-61b0f796b78f@qq.com> });
运行程序,你将如愿以偿收到以下Email。样式可能会有细微误差
上面email中咱们用了外链的图片,咱们也可使用附件的方式,将图片嵌入进去。给附件加个cid
属性便可。
... let mailOptions = { ... html: '<img src="cid:01">', // html body attachments: [ { filename: 'ZenQcode.png', path: path.resolve(__dirname, 'ZenQcode.png'), cid: '01', } ] }; ...
邮件信息通常都不是固定的,咱们能够引入模板引擎对HTML内容进行渲染。
这里咱们使用Ejs:https://github.com/mde/ejs/来作演示
$ npm install ejs --save
ejs具体语法请参看官方文档
先创建一个email.ejs文件
<h1>hello <%= title %></h1> <p><%= desc %></p>
修改咱们的js文件
... const template = ejs.compile(fs.readFileSync(path.resolve(__dirname, 'email.ejs'), 'utf8')); const html = template({ title: 'Ejs', desc: '使用Ejs渲染模板', }); let mailOptions = { from: '"JavaScript之禅" <xxxxx@qq.com>', // sender address to: 'xxxxx@163.com', // list of receivers subject: 'Hello', // Subject line html: html,// html body }; ...
到此,你的邮箱将收到一个动态渲染的hello Ejs。
本文到此告一段落,在此基础上你能够实现更多有用的功能
左手代码,右手砖,抛砖引玉
若是你知道更多好用HTML email资源,留言交流让更多人知道。
…
…
…