node.js發(fā)送郵件email的方法詳解
本文實(shí)例講述了node.js發(fā)送郵件email的方法。分享給大家供大家參考,具體如下:
通常我們做node項(xiàng)目時(shí),可能我們會(huì)碰到做一個(gè)簡(jiǎn)單的郵件反饋,那么我們今天就來(lái)討論一下,其中遇到的各種坑。
總的來(lái)說(shuō)做這個(gè)東西,我們可能需要node第三方依賴模塊,來(lái)實(shí)現(xiàn)我們要達(dá)到的效果。
這里我推薦兩個(gè)模塊:https://github.com/pingfanren/Nodemailer
npm install nodemailer //這個(gè)模塊不錯(cuò),github上星也比較多,還經(jīng)常有維護(hù),但是坑也比較多
另一個(gè),https://github.com/eleith/emailjs
npm install emailjs --save
這里我用的是nodemailer模塊,畢竟用的人比較多,跟隨主流呢
它的特點(diǎn):
使用Unicode編碼
支持Windows系統(tǒng),不需要安裝依賴
支持純文本和HTML格式
支持發(fā)送附件(包括大型附件)
在HTML中嵌入圖片
支持SSL/STARTTLS安全協(xié)議
不同的傳輸方法,可以使用內(nèi)置也可以使用外部插件的形式
提供自定義插件支持(比如增加DKIM簽名,使用markdown代替HTML等等)
支持XOAUTH2登錄驗(yàn)證(以及關(guān)于更新的令牌反饋)
安裝使用
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密碼,是你設(shè)置的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', // 標(biāo)題
//text和html兩者只支持一種
text: 'Hello world ?', // 標(biāo)題
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ā)送郵件成功以后我們很少會(huì)有操作,但也有極少數(shù)情況需要在成功以后會(huì)處理一些特殊信息的,這時(shí)候info對(duì)象就能發(fā)揮余熱了。info對(duì)象中包含了messageId、envelop、accepted和response等屬性,具體看文檔我不一一介紹了。
使用其他傳輸插件 https://github.com/andris9/nodemailer-smtp-transport
npm install nodemailer-smtp-transport --save
其他代碼類似,差別只是在創(chuàng)建transport上,所以這里我就寫(xiě)一部分代碼:
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
// 開(kāi)啟一個(gè) SMTP 連接池
var transport = nodemailer.createTransport(smtpTransport({
host: "smtp.qq.com", // 主機(jī)
secure: true, // 使用 SSL
secureConnection: true, // 使用 SSL
port: 465, // SMTP 端口
auth: {
user: "gaolu19901228@qq.com", // 賬號(hào)
pass: "******" // 密碼
}
}));
// 設(shè)置郵件內(nèi)容
var mailOptions = {
from: "768065158<768065158@qq.com>", // 發(fā)件地址
to: "528779822@qq.com", // 收件列表
subject: "Hello world", // 標(biāo)題
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(); // 如果沒(méi)用,關(guān)閉連接池
});
下面列出了一些發(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格式,附件使用二進(jìn)制流。
附件
附件對(duì)象包含了下面這些屬性:
filename 附件名
content 內(nèi)容
encoding 編碼格式
path 文件路徑
contentType 附件內(nèi)容類型
常見(jiàn)錯(cuò)誤
1.賬號(hào)未設(shè)置該服務(wù)
{ [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郵箱 -> 設(shè)置 -> 帳戶 -> 開(kāi)啟服務(wù):POP3/SMTP服務(wù)
2.發(fā)件賬號(hào)與認(rèn)證賬號(hào)不同
{ [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.登錄認(rèn)證失敗,可能由于smpt獨(dú)立密碼錯(cuò)誤導(dǎo)致 我在qq設(shè)置的時(shí)候就遇到過(guò)
Invalid login - 535 Authentication failed
解決方案:
qq郵箱在測(cè)試smtp郵件服務(wù)器時(shí),一,在qq郵箱,設(shè)置,賬戶設(shè)置中.開(kāi)啟下smtp.二,設(shè)置一下獨(dú)立密碼.三,在配置smtp服務(wù)器的密碼時(shí),注意一定要填你設(shè)置的獨(dú)立密碼.不要用郵箱登錄密碼.否則會(huì)提示535 Authentication failed錯(cuò)誤.
希望本文所述對(duì)大家nodejs程序設(shè)計(jì)有所幫助。
相關(guān)文章
詳解從買域名到使用pm2部署node.js項(xiàng)目全過(guò)程
本篇文章主要介紹了詳解從買域名到使用pm2部署node.js項(xiàng)目全過(guò)程,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Node.js connect ECONNREFUSED錯(cuò)誤解決辦法
這篇文章主要介紹了Node.js connect ECONNREFUSED錯(cuò)誤解決辦法的相關(guān)資料,需要的朋友可以參考下2016-09-09
nodemon實(shí)現(xiàn)Typescript項(xiàng)目熱更新的示例代碼
這篇文章主要介紹了nodemon實(shí)現(xiàn)Typescript項(xiàng)目熱更新的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
NodeJS創(chuàng)建最簡(jiǎn)單的HTTP服務(wù)器
這篇文章主要介紹了NodeJS創(chuàng)建最簡(jiǎn)單的HTTP服務(wù)器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
node?puppeteer爬蟲(chóng)爬取電影網(wǎng)站及生成pdf文檔示例
這篇文章主要介紹了node?puppeteer爬蟲(chóng)爬取電影網(wǎng)站及生成pdf文檔使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Node.js和Vue的安裝與配置超詳細(xì)步驟(推薦)
使用VUE前端框架開(kāi)發(fā),需要安裝Node.js和Vue.js,這篇文章主要給大家介紹了關(guān)于Node.js和Vue的安裝與配置超詳細(xì)步驟的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
nodejs簡(jiǎn)單實(shí)現(xiàn)操作arduino
本文給大家分享的是使用nodejs來(lái)驅(qū)動(dòng)arduino,主要是基于cylonjs 和 gort,有需要的小伙伴可以參考下2016-09-09

