Skip to main content

适用于 Python 的 Microsoft Azure MyService 管理客户端库

项目描述

适用于 Python 的 Azure 通信电子邮件客户端库

此包包含用于 Azure 电子邮件通信服务的 Python SDK。

关键概念

Azure 通信电子邮件包用于执行以下操作:

  • 向多种类型的收件人发送电子邮件
  • 查询已发送邮件的状态

入门

先决条件

您需要一个Azure 订阅、一个通信服务资源和一个具有活动域的电子邮件通信资源

要创建这些资源,您可以使用Azure 门户Azure PowerShell.NET 管理客户端库

安装

使用pip安装适用于 Python 的 Azure 通信电子邮件客户端库:

pip install azure-communication-email

例子

EmailClient提供发送电子邮件的功能。

验证

可以使用从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);

电子邮件客户端也可以使用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);

发送电子邮件

要发送电子邮件,请sendEmailClient.

content = EmailContent(
    subject="This is the subject",
    plain_text="This is the body",
    html= "<html><h1>This is the body</h1></html>",
)

address = EmailAddress(email="customer@domain.com", display_name="Customer Name")

message = EmailMessage(
            sender="sender@contoso.com",
            content=content,
            recipients=EmailRecipients(to=[address])
        )

response = client.send(message)

向多个收件人发送电子邮件

要将电子邮件消息发送给多个收件人,请为每个收件人类型添加一个对象,并为每个收件人添加一个对象。

content = EmailContent(
    subject="This is the subject",
    plain_text="This is the body",
    html= "<html><h1>This is the body</h1></html>",
)

recipients = EmailRecipients(
        to=[
            EmailAddress(email="customer@domain.com", display_name="Customer Name"),
            EmailAddress(email="customer2@domain.com", display_name="Customer Name 2"),
        ],
        cc=[
            EmailAddress(email="ccCustomer@domain.com", display_name="CC Customer Name"),
            EmailAddress(email="ccCustomer2@domain.com", display_name="CC Customer Name 2"),
        ],
        bcc=[
            EmailAddress(email="bccCustomer@domain.com", display_name="BCC Customer Name"),
            EmailAddress(email="bccCustomer2@domain.com", display_name="BCC Customer Name 2"),
        ]
    )

message = EmailMessage(sender="sender@contoso.com", content=content, recipients=recipients)
response = client.send(message)

发送带附件的电子邮件

Azure 通信服务支持发送带有附件的电子邮件。

import base64

content = EmailContent(
    subject="This is the subject",
    plain_text="This is the body",
    html= "<html><h1>This is the body</h1></html>",
)

address = EmailAddress(email="customer@domain.com", display_name="Customer Name")

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

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

attachment = EmailAttachment(
    name="attachment.txt",
    attachment_type="txt",
    content_bytes_base64=file_bytes_b64.decode()
)

message = EmailMessage(
            sender="sender@contoso.com",
            content=content,
            recipients=EmailRecipients(to=[address]),
            attachments=[attachment]
        )

response = client.send(message)

获取电子邮件消息状态

调用的结果send包含一个message_id可用于查询电子邮件状态的。

response = client.send(message)
status = client.get_sent_status(response.message_id)

故障排除

如果对服务器的请求失败,电子邮件操作将引发异常。电子邮件客户端将引发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

本项目采用了微软开源行为准则。有关详细信息,请参阅行为准则常见问题解答或联系opencode@microsoft.com提出任何其他问题或意见。

发布历史

1.0.0b1 (2022-08-09)

Azure 通信电子邮件客户端的第一个预览版具有以下功能:

  • 向多个收件人发送带有附件的电子邮件
  • 获取已发送消息的状态

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

azure-communication-email-1.0.0b1.zip (65.9 kB 查看哈希

已上传 source

内置分布

azure_communication_email-1.0.0b1-py3-none-any.whl (39.7 kB 查看哈希

已上传 py3