|
除了Python,在node中收发电子邮件也非常简单,因为强大的社区有各种各样的包可以供我么直接使用。Nodemailer包就可以帮助我们快速实现发送邮件的功能。
Nodemailer简介
Nodemailer是一个简单易用的Node.js邮件发送组件
官网地址:https://nodemailer.com
GitHub地址:https://github.com/nodemailer/nodemailer
Nodemailer的主要特点包括:
- 支持Unicode编码
- 支持Window系统环境
- 支持HTML内容和普通文本内容
- 支持附件(传送大附件)
- 支持HTML内容中嵌入图片
- 支持SSL/STARTTLS安全的邮件发送
- 支持内置的transport方法和其他插件实现的transport方法
- 支持自定义插件处理消息
- 支持XOAUTH2登录验证
安装使用
首先,我们肯定是要下载安装 注意:Node.js v6+
npm install nodemailer --save
发出个真实的邮件
这里我使用了我的qq邮箱给另一个qq邮箱发送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: 'growvv@qq.com',
// 这里密码不是qq密码,是你设置的smtp授权码,去qq邮箱后台开通、查看
pass: 'xxx',
}
});
let mailOptions = {
from: '"Rogn" <growvv@qq.com>', // sender address
to: '3214739256@qq.com', // list of receivers
subject: 'Hello', // Subject line
// 发送text或者html格式
// text: 'Hello world?', // plain text body
html: '<h1>Hello world</h1>' // 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);
console.log(info)
});
更多的配置像CC、BCC、attachments等可参见 https://nodemailer.com/message/。
参考链接:https://segmentfault.com/a/1190000012251328
个性签名:时间会解决一切
来源:https://www.cnblogs.com/lfri/p/12077559.html |