node.js發送郵件email

  一般咱們作node項目時,可能咱們會碰到作一個簡單的郵件反饋,那麼咱們今天就來討論一下,其中遇到的各類坑。html

  總的來講作這個東西,咱們可能須要node第三方依賴模塊,來實現咱們要達到的效果。node

  這裏我推薦兩個模塊:https://github.com/pingfanren/Nodemailergit

npm install nodemailer   //這個模塊不錯,github上星也比較多,還常常有維護,可是坑也比較多

  另外一個,https://github.com/eleith/emailjsgithub

npm install emailjs  --save 

  這裏我用的是nodemailer模塊,畢竟用的人比較多,跟隨主流呢web

   它的特色:npm

  • 使用Unicode編碼
  • 支持Windows系統,不須要安裝依賴
  • 支持純文本和HTML格式
  • 支持發送附件(包括大型附件)
  • 在HTML中嵌入圖片
  • 支持SSL/STARTTLS安全協議
  • 不一樣的傳輸方法,可使用內置也可使用外部插件的形式
  • 提供自定義插件支持(好比增長DKIM簽名,使用markdown代替HTML等等)
  • 支持XOAUTH2登陸驗證(以及關於更新的令牌反饋)

安裝使用

npm install nodemailer --save

  使用內置傳輸發送郵件,能夠查看支持列表:https://github.com/andris9/nodemailer-wellknown#supported-services 安全

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
    //https://github.com/andris9/nodemailer-wellknown#supported-services 支持列表
    service: 'qq',
    port: 465, // SMTP 端口
    secureConnection: true, // 使用 SSL
    auth: {
        user: '768065158@qq.com',
        //這裏密碼不是qq密碼,是你設置的smtp密碼
        pass: '*****'
    }
});

// NB! No need to recreate the transporter object. You can use
// the same transporter object for all e-mails

// setup e-mail data with unicode symbols
var mailOptions = {
    from: '768065158@qq.com', // 發件地址
    to: '528779822@qq.com', // 收件列表
    subject: 'Hello sir', // 標題
    //text和html二者只支持一種
    text: 'Hello world ?', // 標題
    html: '<b>Hello world ?</b>' // html 內容
};

// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);

});

  發送郵件成功之後咱們不多會有操做,但也有極少數狀況須要在成功之後會處理一些特殊信息的,這時候info對象就能發揮餘熱了。info對象中包含了messageId、envelop、accepted和response等屬性,具體看文檔我不一一介紹了。服務器

  使用其餘傳輸插件   https://github.com/andris9/nodemailer-smtp-transport

npm install nodemailer-smtp-transport  --save

  其餘代碼相似,差異只是在建立transport上,因此這裏我就寫一部分代碼:markdown

var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');

// 開啓一個 SMTP 鏈接池
var transport = nodemailer.createTransport(smtpTransport({
  host: "smtp.qq.com", // 主機
  secure: true, // 使用 SSL
  secureConnection: true, // 使用 SSL
  port: 465, // SMTP 端口
  auth: {
    user: "gaolu19901228@qq.com", // 帳號
    pass: "******" // 密碼
  }
}));
 
// 設置郵件內容
var mailOptions = {
  from: "768065158<768065158@qq.com>", // 發件地址
  to: "528779822@qq.com", // 收件列表
  subject: "Hello world", // 標題
  text:"hello",
  html: "<b>thanks a for visiting!</b> 世界,你好!" // html 內容
}
 
// 發送郵件
transport.sendMail(mailOptions, function(error, response) {
  if (error) {
    console.error(error);
  } else {
    console.log(response);
  }
  transport.close(); // 若是沒用,關閉鏈接池
});

  下面列出了一些發郵件的字段:app

  • from 發送者郵箱
  • sender 發送者區域顯示的信息
  • to 接收者郵箱
  • cc 抄送者郵箱
  • bcc 密送者郵箱
  • subject 郵箱主題
  • attachments 附件內容
  • watchHtml apple watch指定的html版本
  • text 文本信息
  • html html內容
  • headers 另加頭信息
  • encoding 編碼格式

  郵件內容使用UTF-8格式,附件使用二進制流。

  附件

  附件對象包含了下面這些屬性:

  • filename 附件名
  • content 內容
  • encoding 編碼格式
  • path 文件路徑
  • contentType 附件內容類型 

常見錯誤

  1.帳號未設置該服務

{ [AuthError: Invalid login - 454 Authentication failed, please open smtp flag first!]
  name: 'AuthError',
  data: '454 Authentication failed, please open smtp flag first!',
  stage: 'auth' }

  解決方案:
  QQ郵箱 -> 設置 -> 賬戶 -> 開啓服務:POP3/SMTP服務

  2.發件帳號與認證帳號不一樣

{ [SenderError: Mail from command failed - 501 mail from address must be same as authorization user]
name: 'SenderError',
data: '501 mail from address must be same as authorization user',
stage: 'mail' }

  3.登陸認證失敗,可能因爲smpt獨立密碼錯誤致使 我在qq設置的時候就遇到過

Invalid login - 535 Authentication failed 

  解決方案:

  qq郵箱在測試smtp郵件服務器時,一,在qq郵箱,設置,帳戶設置中.開啓下smtp.二,設置一下獨立密碼.三,在配置smtp服務器的密碼時,注意必定要填你設置的獨立密碼.不要用郵箱登陸密碼.不然會提示535 Authentication failed錯誤.

  

 

資料參考:

  qq郵箱smpt設置獨立密碼:http://wap.yzmg.com/app/103426.html

  nodejs使用 nodemailer發送郵件

  Nodejs發郵件組件Nodemailer

  用nodemailer組件發郵件

  nodemailer使用過程當中發現的一些問題

  nodeemail wellkown

相關文章
相關標籤/搜索