适用于 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);
发送电子邮件
要发送电子邮件,请send
从EmailClient
.
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的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 05eb0bab42893a120efe2e3895b46f0abdbcc8810badae71dde0de6c6391c454 |
|
MD5 | f17737a6cca007d6c0e463a8bf4f7ead |
|
布莱克2-256 | 0390b74fab9ff9fecf350eb62e70af66701eb4796990eeeed6d0e2b40085722d |
azure_communication_email -1.0.0b1-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | cb28d1bf82785bde2c86c0f5495e9a333a7e840c4cf6ae54b88eaec062cb39d3 |
|
MD5 | e4c2fe5bac028619faa4276fb2115e9c |
|
布莱克2-256 | c28b6f4e59ffc45770391456543fb92ff1b34e2a3039f651dbec8c22330d3b4c |