码迷,mamicode.com
首页 > 其他好文 > 详细

node第三方模块----nodemailer发送邮件

时间:2019-06-03 17:22:40      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:nod   subject   相关信息   rate   known   function   save   修改   多个   

参考地址

https://nodemailer.com/about/

nodemailer

nodemailer是一个nodejs的邮件服务模块

如何使用nodemailer

1.先安装nodemailer

npm install --save nodemailer 

2.使用nodemailer

"use strict";
//引入第三方模块
const nodemailer = require("nodemailer");
// 创建发送邮件的对象
let transporter = nodemailer.createTransport({
    //node_modules/nodemailer/lib/well-known/services.json  查看相关的配置,进行修改,如果使用qq邮箱,就查看qq邮箱的相关配置
    host: "smtp.qq.com",
    port: 465,
  //端口号为465时,值为true,其他为false secure:
true, auth: { //发送者邮箱 user: 2252281518@qq.com, //pass 不是邮箱账户的密码而是stmp的授权码(必须是相应邮箱的stmp授权码) //邮箱---设置--账户--POP3/SMTP服务---开启---获取stmp授权码 pass: qjmtfzzsotlzecbf } }); // 邮件的相关信息 let msg = { //发送者邮箱 from: 2252281518@qq.com, //接收者邮箱,多个邮箱用逗号间隔 to: "2252281518@qq.com", //邮件标题 subject: "Hello ?", //文件内容,发送文件是text格式或者html格式,二者只能选择一个 // text: "Hello world?", // plain text body html: "<h1>欢迎您注册啦!</h1>" // html body } // 发送邮件 transporter.sendMail(msg, (err, res) => { console.log(err); console.log(res); })

 3.封装成独立的模块mail.js,

 直接调用sendMail("接收者邮箱","邮件内容")即可使用

"use strict";
//引入第三方模块
const nodemailer = require("nodemailer");
//
function sendMail(mail,code) {
    // 创建发送邮件的对象
    let transporter = nodemailer.createTransport({
        //node_modules/nodemailer/lib/well-known/services.json  查看相关的配置,如果使用qq邮箱,就查看qq邮箱的相关配置
        host: "smtp.qq.com",
        port: 465,
        secure: true, // true for 465, false for other ports
        auth: {
            //发送者邮箱
            user: 2252281518@qq.com, // generated ethereal user
            //pass 不是邮箱账户的密码而是stmp的授权码(必须是相应邮箱的stmp授权码)
            //邮箱---设置--账户--POP3/SMTP服务 开启--成功开启POP3/SMTP服务,在第三方客户端登录时,密码框请输入以下授权码:
            pass: qjmtfzzsotlzecbf
        }
    });

    // 邮件的相关信息
    let msg = {
        //发送者邮箱
        from: 2252281518@qq.com, // sender address
        //接收者邮箱,多个邮箱用逗号间隔
        to: mail, // list of receivers
        //邮件标题
        subject: "邮箱验证啦", 
        //文件内容,发送文件是text格式或者html格式,二者只能选择一个
        // text: "Hello world?", // plain text body
        html: code 
    }

    // 发送邮件
    transporter.sendMail(msg, (err, res) => {
        console.log(err);
        console.log(res);
    })
}
module.exports = sendMail;

使用

//引入封装好的模块
const
sendmail = require("./mail.js");
//调用 sendmail(
2252281518@qq.com,<H1>请验收</H1>);

 

node第三方模块----nodemailer发送邮件

标签:nod   subject   相关信息   rate   known   function   save   修改   多个   

原文地址:https://www.cnblogs.com/SRH151219/p/10968600.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!