適用于 Python 的 Azure 通訊Email用戶端程式庫 - 1.0.0 版

此套件包含適用于 Email Azure 通訊服務 的 Python SDK。

重要概念

Azure 通訊Email套件是用來傳送電子郵件給多種收件者類型。

開始使用

Prerequisites

您需要Azure 訂用帳戶、通訊服務資源,以及具有作用中網域Email通訊資源

若要建立這些資源,您可以使用Azure 入口網站Azure PowerShell.NET 管理用戶端程式庫

安裝

使用pip安裝適用于 Python 的 Azure Communication Email 用戶端程式庫:

pip install azure-communication-email

範例

EmailClient 提供傳送電子郵件訊息的功能。

驗證

Email用戶端可以使用從Azure入口網站中的 Azure 通訊資源取得的連接字串進行驗證。

from azure.communication.email import EmailClient

connection_string = "endpoint=https://<resource-name>.communication.azure.com/;accessKey=<Base64-Encoded-Key>"
client = EmailClient.from_connection_string(connection_string);

或者,您也可以使用 DefaultAzureCredential 來使用 Active Directory 驗證。

from azure.communication.email import EmailClient
from azure.identity import DefaultAzureCredential

# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables.
endpoint = "https://<resource-name>.communication.azure.com"
client = EmailClient(endpoint, DefaultAzureCredential())

Email用戶端也可以使用AzureKeyCredential 進行驗證。

from azure.communication.email import EmailClient
from azure.core.credentials import AzureKeyCredential

credential = AzureKeyCredential("<api_key>")
endpoint = "https://<resource-name>.communication.azure.com/"
client = EmailClient(endpoint, credential);

傳送Email訊息

若要傳送電子郵件訊息,請從 EmailClient 呼叫 函 begin_send 式。 這會傳回輪詢器。 您可以使用此輪詢器來檢查作業的狀態,並在作業完成後擷取結果。

message = {
    "content": {
        "subject": "This is the subject",
        "plainText": "This is the body",
        "html": "<html><h1>This is the body</h1></html>"
    },
    "recipients": {
        "to": [
            {
                "address": "customer@domain.com",
                "displayName": "Customer Name"
            }
        ]
    },
    "senderAddress": "sender@contoso.com"
}

poller = email_client.begin_send(message)
result = poller.result()

將Email訊息傳送給多個收件者

若要將電子郵件訊息傳送給多個收件者,請為每個收件者類型和每個收件者新增物件。

message = {
    "content": {
        "subject": "This is the subject",
        "plainText": "This is the body",
        "html": "<html><h1>This is the body</h1></html>"
    },
    "recipients": {
        "to": [
            {"address": "customer@domain.com", "displayName": "Customer Name"},
            {"address": "customer2@domain.com", "displayName": "Customer Name 2"}
        ],
        "cc": [
            {"address": "ccCustomer@domain.com", "displayName": "CC Customer Name"},
            {"address": "ccCustomer2@domain.com", "displayName": "CC Customer Name 2"}
        ],
        "bcc": [
            {"address": "bccCustomer@domain.com", "displayName": "BCC Customer Name"},
            {"address": "bccCustomer2@domain.com", "displayName": "BCC Customer Name 2"}
        ]
    },
    "senderAddress": "sender@contoso.com"
}

poller = email_client.begin_send(message)
result = poller.result()

使用附件傳送Email

Azure 通訊服務支援傳送含有附件的電子郵件。

import base64

with open("C://readme.txt", "r") as file:
    file_contents = file.read()

file_bytes_b64 = base64.b64encode(bytes(file_contents, 'utf-8'))

message = {
    "content": {
        "subject": "This is the subject",
        "plainText": "This is the body",
        "html": "<html><h1>This is the body</h1></html>"
    },
    "recipients": {
        "to": [
            {
                "address": "customer@domain.com",
                "displayName": "Customer Name"
            }
        ]
    },
    "senderAddress": "sender@contoso.com",
    "attachments": [
        {
            "name": "attachment.txt",
            "attachmentType": "text/plain",
            "contentInBase64": file_bytes_b64.decode()
        }
    ]
}

poller = email_client.begin_send(message)
result = poller.result()

疑難排解

如果對伺服器的要求失敗,Email作業將會擲回例外狀況。 Email用戶端會引發Azure Core中定義的例外狀況。

from azure.core.exceptions import HttpResponseError

try:
    response = email_client.send(message)
except HttpResponseError as ex:
    print('Exception:')
    print(ex)

下一步

參與

此專案歡迎參與和提供建議。 大部分的參與都要求您同意「參與者授權合約 (CLA)」,宣告您有權且確實授與我們使用投稿的權利。 如需詳細資訊,請造訪 cla.microsoft.com

此專案採用 Microsoft Open Source Code of Conduct (Microsoft 開放原始碼管理辦法)。 如需詳細資訊,請參閱管理辦法常見問題集,如有任何其他問題或意見請連絡 opencode@microsoft.com