node 14.20.1
步驟
- 申請 Google 認證模式OAuth以上獲得
- CLINENT_ID
- CLINENT_SECRET
- REFRESHTOKEN
- ACCESSTOKEN
- 安裝nodemailer
一.申請 Google 認證模式OAuth
一.Google 更改認證模式,Nodemailer 官方文件 也推薦改使用 OAuth 2.0 來介接 Gmail
建立 OAuth
Console
Cloud Google
- 建立一個新專案
2.專案名
3.Api服務,搜尋Gmail Api
4.啟用Gmail Api
5.憑證->Auth 用戶端 ID
6選取外部
測試使用者
憑證 → 建立憑證 → OAuth 用戶端 ID

https://developers.google.com/oauthplayground

接下來請開啟這個網址這裡,然後點一下右上齒輪,並將「Use your own OAuth credentials」打勾輸入剛剛的 Client ID 以及 Client Secret
Step2 取得Token
以上獲得
- CLINENT_ID
- CLINENT_SECRET
- REFRESHTOKEN
- ACCESSTOKEN
二.nodemailer:發信功能串接
安裝 nodemailer
nodemailer官網
1 | npm install --save nodemailer |
新增一個頁面router
1 | // routes |
到contact 頁面
1 | var express = require('express'); |
token 有失效的問題
// 完整程式碼https://github.com/hsiangfeng/node-nodemailer-finish
//https://hackmd.io/@yy933/Hy9rKyAE3?utm_source=preview-mode&utm_medium=rec
參考文獻
全端勇士之路 Node.js-OAuth 2.0 & nodemailer & Gmail
Send mails from Gmail using Nodemaile
Sending Emails with Node.js Using SMTP, Gmail, and OAuth2
Sending Emails with Node.js Using SMTP, Gmail, and OAuth2
Use Expressjs to send mails with gmail OAuth 2.0 and nodemailer
OAUTH2
Nodemailer/Gmail - What exactly is a refresh token and how do I get one?