【 Cloud 】使用 Azure Communication Service 寄送 Email ( Python )

【 Cloud 】使用 Azure Communication Service 寄送 Email ( Python )
Photo by karimjy LOULOUA / Unsplash

內容

  • 學習目標
  • 前置準備作業
  • 建立服務所使用的名稱
  • 建立與設定 Azure Communication Service
  • 修改程式
  • 測試與驗證

學習目標

  • 如何透過 Azure Communication Service 與 Python 寄送信件。

前置準備作業

建立服務所使用的名稱

屬性 名稱
Resource group communication-rg
Communication Service communication-svc-168
Email Communication Service mail-communication-svc-168

建立與設定 Azure Communication Service

Step 1. 建立 Azure Communication Service

  • 於搜尋框輸入 Communication Service > 點選 Communication Services 的搜尋結果

---2023-06-12---8.39.15


Step 2. 點選左上角 + Create 按鈕

---2023-06-12---8.42.32


Step 3. 輸入相關資訊

  • Resource group 欄位請選擇唯一且可識別的名稱。
  • Data location 欄位本範例選擇 United States
  • Resource Name 欄位請選擇唯一且可識別的名稱。
  • 以上資訊輸入完成後點選左下角的 Review + Create

---2023-06-12---8.44.21

---2023-06-12---8.47.48


Step 4. 確認相關資訊

  • 確認完成後點選 Create

---2023-06-12---8.50.22


Step 5. 建立過程中

---2023-06-12---8.51.46


Step 6. 建立完成,點選 Go to resource

---2023-06-12---8.52.59


Step 7. 建立 Domain

  • 點選左側的 Email 中的 Domains > 再點選 Connect domain

---2023-06-12---8.55.37

  • 點選 Add an email service

---2023-06-12---8.57.27

  • 輸入相關資訊後,再點選 Review + create

---2023-06-12---8.59.03

---2023-06-12---8.59.44

  • 確認相關資訊後,再點選 Create

---2023-06-12---9.01.40

  • 建立過程中

---2023-06-12---9.02.46

  • 建立完成,點選 Go to resource

---2023-06-12---9.04.24

  • 設定 Domain
    • 請依需求選擇使用 Azure subdomaincustom domain,本範例使用 Azure subdomain

---2023-06-12---9.07.53

  • domain 設定完成,請點選所建立完成的 domain

---2023-06-12---9.10.48

  • 取得 MailFrom

---2023-06-12---9.12.42

  • 回到所建立的 Azure Communication Service > 點選左側的 Email 中的 Domains > 再點選 Connect domain

---2023-06-12---8.55.37

  • 選擇所建立的 Email Service

---2023-06-12---9.16.14

  • 點選 Connect

---2023-06-12---9.17.39

  • 連結完成

---2023-06-12---9.19.49


Step 8. 取得 Connection string

  • 點選左側的 Keys

---2023-06-12---9.20.52

修改程式

Step 1. 下載程式


Step 2. 建立環境變數

  • 於專案的根目錄中新增 .env 並輸入下方內容
    • 第 1 行的 AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING 的等號後方請輸入 建立與設定 Azure Communication Service 段落的 Step 8 所取得的 Connection string
    • 第 2 行的 AZURE_COMMUNICATION_SERVICE_SENDER_ADDRESS 的等號後方請輸入 建立與設定 Azure Communication Service 段落的 Step 7 所取得的 MailFrom

Step 3. 安裝相關套件

  • 請使用終端機切換工作路徑到專案目錄中並輸入下方指令
pip install azure-communication-email python-dotenv

測試與驗證

Step 1. 修改收件者相關資訊

  • 請修改第 68 與 69 行成所要接收信件的人

Step 2. 執行程式

  • 請使用終端機輸入下方指令
python app.py

---2023-06-12---1.16.30


Step 3. 到收件者信箱收信

---2023-06-12---1.16.57

Reference

Sample Code

GitHub

List of blogs