1. 程式人生 > 其它 >nodemailer郵件服務配置

nodemailer郵件服務配置

技術標籤:記錄node.js

nodemailer郵件服務配置

前言

最近剛學的郵件服務配置,其實我只知道如何配置。如果有不對的還請指正


一、nodemailer是什麼?

Nodemailer is a module for Node.js applications to allow easy as cake email sending. The project got started back in 2010 when there was no sane option to send email messages, today it is the solution most Node.js users turn to by default.

Nodemailer是一個模組,為Node.js應用程式,讓輕鬆的蛋糕郵件傳送。這個專案開始於2010年,當時還沒有傳送電子郵件訊息的明智選擇,但現在它是大多數Node.js使用者預設使用的解決方案。(有道翻譯)

二、使用步驟

1. 寫程式碼之前先去郵箱裡開通SMTP/IMAP服務,我用的是網易郵箱

163

2. 程式碼實現

// npm install nodemailer

import nodemailer from 'nodemailer'

async function main() {

  let transporter = nodemailer.createTransport({
    service:
'163', // 代號吧 設定後無需設定主機埠選項 詳情https://nodemailer.com/smtp/well-known/ auth: { user:'[email protected]', // 傳送郵箱號 pass: 'QTSWLOOPCWQNUFMR', // 生成的密碼 }, }); // 使用定義的傳輸物件傳送郵件 let info = await transporter.sendMail({ from: '"認證郵件"<[email protected]>', // 傳送地址 to:
"[email protected]", // 接收郵箱 subject: "Hello 修改你的密碼啦 ✔", // 標題 html: `<div style='width: 500px; height:none; margin: 0 auto; box-sizing: border-box; border: 1px solid #000;'> <h1 style='font-size: 20px; margin: 0; width: 100%; padding: 10px; box-sizing: border-box; background-color: #009e94;'> &lt; huang &gt; </h1> <div style='padding: 20px; box-sizing: border-box;'> <p style='margin: 0;'> 尊敬的使用者名稱替換使用者您好</p> <p style='text-indent: 2em;'> 您的重置連結有效時間為30分鐘,請在時間替換之前重置您的密碼。<a href='網址替換'>立即重置密碼</a></p> <br /> <div style='padding: 5px; background: #f2f2f2; font-size: 12px;'>如果該郵件不是由你本人操作,請勿進行啟用!否則你的郵箱將會被他人繫結。</div> <div style='background: #fafafa; color: #b4b4b4;text-align: center; line-height: 45px; height: 45px; bottom: 0;width: 100%; font-size: 14px;'> 系統郵件,請勿直接回復</div> </div> </div>`, // html body html主體 }); console.log("Message sent: %s", info.messageId); console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info)); } main().catch(console.error);

總結

以上,真的就是一點皮毛更多詳情移步官網檢視https://nodemailer.com/about/