找nodejs库的时候找到了这个框架,感觉应该会满常用,先记录一下
npm install nodemailer
具体发送demo
- const nodemailer = require('nodemailer');
-
- nodemailer.createTestAccount((err, account) => {
- // create reusable transporter object using the default SMTP transport
- let transporter = nodemailer.createTransport({
- host: 'smtp.qq.com',//QQ邮箱的SMTP服务器
- port: 587,//QQ邮箱的SMTP服务器的端口为465或587
- secure: false, // true for 465, false for other ports
- auth: {
- user: 'sender@qq.com', // 刚刚申请授权码的邮箱账号
- pass: 'QQ邮箱申请一下,在设置/账号里进行申请' // 刚刚申请的授权码
- }
- });
-
- // messageOptions配置
- let message = {
- from: 'sender@qq.com', // 这里必须是刚刚申请授权码的邮箱账号
- to: 'receiver@qq.com', // 收件邮箱
- subject: '代码发送邮件测试', // 邮件title
- text: 'Hello!', // 有html,优先显示html,没有html就显示这个
- html: `嗨嗨嗨!`, // html body
- attachments: '' // 附件
- };
-
- // 邮件发送
- transporter.sendMail(message , (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));
- console.log('message sent successfully!');
- });
- });