通常我們做node項目時,可能我們會碰到做一個簡單的郵件反饋,那么我們今天就來討論一下,其中遇到的各種坑。
總的來說做這個東西,我們可能需要node第三方依賴模塊,來實現我們要達到的效果。
這里我推薦兩個模塊:https://github.com/pingfanren/Nodemailer
npm install nodemailer //這個模塊不錯,github上星也比較多,還經常有維護,但是坑也比較多
另一個,https://github.com/eleith/emailjs
npm install emailjs --save
這里我用的是nodemailer模塊,畢竟用的人比較多,跟隨主流呢
它的特點:
- 使用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上,所以這里我就寫一部分代碼:
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(); // 如果沒用,關閉連接池 });
下面列出了一些發郵件的字段:
- 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