跳至主要內容

使用 SendGrid Email 設定電子郵件驗證

Logto 的官方 SendGrid 電子郵件服務連接器。

提示:

本指南假設你已對 Logto 連接器 (Connectors) 有基本了解。若不熟悉,請參閱 連接器 (Connectors) 指南以開始使用。

開始使用

SendGrid(即 Twilio SendGrid)是一個用於交易和行銷電子郵件的客戶通信平台。我們可以使用其電子郵件發送功能來發送 驗證碼

註冊 SendGrid 帳號

前往 SendGrid 網站 建立新帳號。如果你已經有帳號,可以跳過此步驟。

驗證寄件人

進入 SendGrid 控制台頁面,並使用你的 SendGrid 帳號登入。

寄件人代表我們發送驗證碼郵件時所使用的寄件地址。為了透過 SendGrid 郵件伺服器發送郵件,你至少需要驗證一個寄件人。

SendGrid 控制台頁面 開始,於側邊欄選擇「Settings」->「Sender Authentication」。

建議進行網域驗證(Domain Authentication),但不是強制要求。你可以點擊「Authenticate Your Domain」卡片中的「Get started」,並依照接下來的指引將寄件人連結並驗證至 SendGrid。

點擊面板中的「Verify a Single Sender」按鈕後,會看到一個需要填寫關鍵資訊的表單來建立寄件人。請依照指引填寫所有欄位,然後點擊「Create」按鈕。

單一寄件人建立完成後,系統會寄送一封帶有驗證連結的郵件到你的寄件人信箱。請前往信箱,找到驗證郵件並點擊信中的連結完成單一寄件人驗證。現在你就可以使用剛剛驗證的寄件人透過 SendGrid 連接器發送郵件。

建立 API 金鑰

SendGrid 控制台頁面 開始,於側邊欄選擇「Settings」->「API Keys」。

在 API Keys 頁面右上角點擊「Create API Key」。輸入 API 金鑰名稱,並根據你的使用情境自訂「API Key Permission」。若要使用此 API 金鑰發送郵件,必須擁有全域 Full AccessRestricted Access(但需對 Mail Send 擁有完整存取權)。

完成 Create API Key 流程後,API 金鑰會立即顯示在畫面上。請務必將此 API 金鑰妥善保存,因為這是唯一一次能看到它的機會。

設定你的連接器

將「建立 API 金鑰」步驟中取得的 API Key 填入 apiKey 欄位。

將寄件人的 From AddressNickname 分別填入 fromEmailfromName 欄位。你可以在 "Sender Management" 頁面 找到寄件人詳細資訊。fromName 為選填欄位,可視情況略過。

你可以針對不同情境新增多個 SendGrid 郵件連接器範本。以下為新增單一範本的範例:

  • 填寫 subject 欄位,作為郵件標題。
  • content 欄位填入任意字串內容。請務必保留 {{code}} 佔位符以插入隨機驗證碼。
  • usageType 欄位請根據用途填入 RegisterSignInForgotPasswordGeneric
  • type 欄位請填入 text/plaintext/html,以指定內容格式。

為了啟用完整的使用者流程,必須提供 RegisterSignInForgotPasswordGeneric 這四種 usageType 的範本。

以下為 SendGrid 連接器範本 JSON 範例。

[
{
"subject": "<register-template-subject>",
"content": "<Logto: Your verification code is {{code}}. (register template)>",
"usageType": "Register",
"type": "text/plain",
},
{
"subject": "<sign-in-template-subject>",
"content": "<Logto: Your verification code is {{code}}. (sign-in template)>",
"usageType": "SignIn",
"type": "text/plain",
},
{
"subject": "<forgot-password-template-subject>",
"content": "<Logto: Your verification code is {{code}}. (forgot-password template)>",
"usageType": "ForgotPassword",
"type": "text/plain",
},
{
"subject": "<generic-template-subject>",
"content": "<Logto: Your verification code is {{code}}. (generic template)>",
"usageType": "Generic",
"type": "text/plain",
},
]

測試 SendGrid 郵件連接器

你可以在「Save and Done」前,輸入一個電子郵件地址並點擊「Send」來測試設定是否正確。

就是這麼簡單。別忘了 在註冊 / 登入流程中啟用連接器

設定類型

名稱類型
apiKeystring
fromEmailstring
fromNamestring (OPTIONAL)
templatesTemplate[]
範本屬性類型列舉值
subjectstringN/A
contentstringN/A
usageTypeenum string'Register' / 'SignIn' / 'ForgotPassword' / 'Generic'
typeenum string'text/plain' / 'text/html'