node-mailer使用

安裝

npm i nodemailer
新建mail.js文件

"use strict";
const nodemailer = require("nodemailer");
const transporter = nodemailer.createTransport({
  host: "smtp.qq.com",
  port: 465,
  secure: true, 
  auth: {
    user: '[email protected]', // 發送方的郵箱地址 
    pass: 'xwjfinrzncyeeacj'
  }
})
const info = {
  from: '"Fred Foo 👻" <[email protected]>', 
  to: "[email protected]",
  subject: "subject"
  // text: "",
  // html: "<b>Hello world?</b>" // 和text只能選擇一個
}
const mail = {
  sendMail: function(ob = {}) {
    transporter.sendMail({...info, ...ob}, (err) => {
      err && console.log(err)
    })
  }
}

module.exports = mail

使用

const mail = require('./mail')
mail.sendMail({
  subject: "這是subject", // 可以不傳
  html: "<h1>這是html</h1>"
})
// 或者
mail.sendMail({
  subject: "這是subject",
  text: "<h1>這是text</h1>"
})
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章