node.js發(fā)送郵件email的方法詳解
本文實例講述了node.js發(fā)送郵件email的方法。分享給大家供大家參考,具體如下:
通常我們做node項目時,可能我們會碰到做一個簡單的郵件反饋,那么我們今天就來討論一下,其中遇到的各種坑。
總的來說做這個東西,我們可能需要node第三方依賴模塊,來實現(xiàn)我們要達到的效果。
這里我推薦兩個模塊:https://github.com/pingfanren/Nodemailer
npm install nodemailer //這個模塊不錯,github上星也比較多,還經(jīng)常有維護,但是坑也比較多
另一個,https://github.com/eleith/emailjs
npm install emailjs --save
這里我用的是nodemailer模塊,畢竟用的人比較多,跟隨主流呢
它的特點:
使用Unicode編碼
支持Windows系統(tǒng),不需要安裝依賴
支持純文本和HTML格式
支持發(fā)送附件(包括大型附件)
在HTML中嵌入圖片
支持SSL/STARTTLS安全協(xié)議
不同的傳輸方法,可以使用內(nèi)置也可以使用外部插件的形式
提供自定義插件支持(比如增加DKIM簽名,使用markdown代替HTML等等)
支持XOAUTH2登錄驗證(以及關于更新的令牌反饋)
安裝使用
npm install nodemailer --save
使用內(nèi)置傳輸發(fā)送郵件,可以查看支持列表: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', // 發(fā)件地址
to: '528779822@qq.com', // 收件列表
subject: 'Hello sir', // 標題
//text和html兩者只支持一種
text: 'Hello world ?', // 標題
html: '<b>Hello world ?</b>' // html 內(nèi)容
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
發(fā)送郵件成功以后我們很少會有操作,但也有極少數(shù)情況需要在成功以后會處理一些特殊信息的,這時候info對象就能發(fā)揮余熱了。info對象中包含了messageId、envelop、accepted和response等屬性,具體看文檔我不一一介紹了。
使用其他傳輸插件 https://github.com/andris9/nodemailer-smtp-transport
npm install nodemailer-smtp-transport --save
其他代碼類似,差別只是在創(chuàng)建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: "******" // 密碼
}
}));
// 設置郵件內(nèi)容
var mailOptions = {
from: "768065158<768065158@qq.com>", // 發(fā)件地址
to: "528779822@qq.com", // 收件列表
subject: "Hello world", // 標題
text:"hello",
html: "<b>thanks a for visiting!</b> 世界,你好!" // html 內(nèi)容
}
// 發(fā)送郵件
transport.sendMail(mailOptions, function(error, response) {
if (error) {
console.error(error);
} else {
console.log(response);
}
transport.close(); // 如果沒用,關閉連接池
});
下面列出了一些發(fā)郵件的字段:
from 發(fā)送者郵箱
sender 發(fā)送者區(qū)域顯示的信息
to 接收者郵箱
cc 抄送者郵箱
bcc 密送者郵箱
subject 郵箱主題
attachments 附件內(nèi)容
watchHtml apple watch指定的html版本
text 文本信息
html html內(nèi)容
headers 另加頭信息
encoding 編碼格式
郵件內(nèi)容使用UTF-8格式,附件使用二進制流。
附件
附件對象包含了下面這些屬性:
filename 附件名
content 內(nèi)容
encoding 編碼格式
path 文件路徑
contentType 附件內(nèi)容類型
常見錯誤
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.發(fā)件賬號與認證賬號不同
{ [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錯誤.
希望本文所述對大家nodejs程序設計有所幫助。
相關文章
Node.js connect ECONNREFUSED錯誤解決辦法
這篇文章主要介紹了Node.js connect ECONNREFUSED錯誤解決辦法的相關資料,需要的朋友可以參考下2016-09-09
nodemon實現(xiàn)Typescript項目熱更新的示例代碼
這篇文章主要介紹了nodemon實現(xiàn)Typescript項目熱更新的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
node?puppeteer爬蟲爬取電影網(wǎng)站及生成pdf文檔示例
這篇文章主要介紹了node?puppeteer爬蟲爬取電影網(wǎng)站及生成pdf文檔使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07

