Package: azure-communication-email
Azure Communication Email client library for PythonThis package contains a Python SDK for Azure Communication Services for Email.Key conceptsThe Azure Communication Email package is used to send emails to multiple types of recipients.Getting startedPrerequisitesYou need anAzure subscription, aCommunication Service Resource, and anEmail Communication Resourcewith an activeDomain.To create these resource, you can use theAzure Portal, theAzure PowerShell, or the.NET management client library.InstallingInstall the Azure Communication Email client library for Python withpip:pipinstallazure-communication-emailExamplesEmailClientprovides the functionality to send email messages.AuthenticationEmail clients can be authenticated using the connection string acquired from an Azure Communication Resource in theAzure Portal.fromazure.communication.emailimportEmailClientconnection_string="endpoint=https://<resource-name>.communication.azure.com/;accessKey=<Base64-Encoded-Key>"client=EmailClient.from_connection_string(connection_string);Alternatively, you can also use Active Directory authentication using DefaultAzureCredential.fromazure.communication.emailimportEmailClientfromazure.identityimportDefaultAzureCredential# 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 clients can also be authenticated using anAzureKeyCredential.fromazure.communication.emailimportEmailClientfromazure.core.credentialsimportAzureKeyCredentialcredential=AzureKeyCredential("<api_key>")endpoint="https://<resource-name>.communication.azure.com/"client=EmailClient(endpoint,credential);Send an Email MessageTo send an email message, call thebegin_sendfunction from theEmailClient. This will return a poller. You can use this poller to check on the status of the operation and retrieve the result once it's finished.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()Send an Email Message to Multiple RecipientsTo send an email message to multiple recipients, add a object for each recipient type and an object for each recipient.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()Send Email with AttachmentsAzure Communication Services support sending email with attachments.importbase64withopen("C://readme.txt","r")asfile: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()TroubleshootingEmail operations will throw an exception if the request to the server fails. The Email client will raise exceptions defined inAzure Core.fromazure.core.exceptionsimportHttpResponseErrortry:response=email_client.send(message)exceptHttpResponseErrorasex:print('Exception:')print(ex)Next stepsRead more about Email in Azure Communication ServicesContributingThis project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visitcla.microsoft.com.This project has adopted theMicrosoft Open Source Code of Conduct. For more information see theCode of Conduct FAQor contactopencode@microsoft.comwith any additional questions or comments.Release History1.0.0 (2023-03-31)Features AddedThe public release of the Azure Communication Services SDK for Email has the following features:send emails with a variety of options (multiple recipients, attachments, etc.)poll for the status of the email that was sent to track its progress1.0.0b2 (2023-03-01)Features AddedAdding support for AAD token authenticationAdded the ability to specify the API version by an optionalapi_versionkeyword parameter.Breaking ChangesMade the SDK Model-less. Objects are now constructed using a dictionary instead of a model.Reworked the SDK to follow the LRO (long running operation) approach. The 'begin_send' method returns a poller that can be used to check for the status of sending the email and retrieve the result. The return object has been adjusted to fit this approach.Theget_send_statusmethod has been removed.Thesenderproperty has been changed tosenderAddress.Theemailproperty under the recipient object has been changed toaddress.TheattachmentTypeproperty underattachmentshas been changed to 'contentType'. This now accepts the attachment mime type.ThecontentBytesBase64property underattachmentshas been changed tocontentInBase64Custom headers in the email message are now key/value pairs.The importance property was removed. Email importance can now be specified through either thex-priorityorx-msmail-prioritycustom headers.Other ChangesPython 3.6 is no longer supported. Please use Python version 3.7 or later. For more details, please read our page onAzure SDK for Python version support policy.1.0.0b1 (2022-08-09)The first preview of the Azure Communication Email Client has the following features:send emails to multiple recipients with attachmentsget the status of a sent message