|
| 1 | +--- |
| 2 | +title: include file |
| 3 | +description: include file |
| 4 | +author: yogeshmo |
| 5 | +manager: koagbakp |
| 6 | +services: azure-communication-services |
| 7 | +ms.author: ymohanraj |
| 8 | +ms.date: 09/08/2022 |
| 9 | +ms.topic: include |
| 10 | +ms.service: azure-communication-services |
| 11 | +ms.custom: private_preview, event-tier1-build-2022 |
| 12 | +--- |
| 13 | + |
| 14 | +Get started with Azure Communication Services by using the Communication Services Python Email SDK to send Email messages. |
| 15 | + |
| 16 | +Completing this quick start incurs a small cost of a few USD cents or less in your Azure account. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 21 | +- [Python](https://www.python.org/downloads/) 2.7 or 3.6+. |
| 22 | +- An Azure Email Communication Services resource created and ready with a provisioned domain. [Get started with creating an Email Communication Resource](../create-email-communication-resource.md). |
| 23 | +- An active Azure Communication Services resource connected to an Email Domain and its connection string. [Get started by connecting an Email Communication Resource with a Azure Communication Resource](../connect-email-communication-resource.md). |
| 24 | + |
| 25 | +### Prerequisite check |
| 26 | +- In a terminal or command window, run the `python --version` command to check that Python is installed. |
| 27 | +- To view the domains verified with your Email Communication Services resource, sign in to the [Azure portal](https://portal.azure.com/). Locate your Email Communication Services resource and open the **Provision domains** tab from the left navigation pane. |
| 28 | + |
| 29 | +## Set up the application environment |
| 30 | + |
| 31 | +To set up an environment for sending emails, take the steps in the following sections. |
| 32 | + |
| 33 | +### Create a new Python application |
| 34 | + |
| 35 | +1. Open your terminal or command window. Then use the following command to create a new directory for your app and navigate to it. |
| 36 | + |
| 37 | + ```console |
| 38 | + mkdir email-quickstart && cd email-quickstart |
| 39 | + ``` |
| 40 | + |
| 41 | +2. Use a text editor to create a file called **send-email.py** in the project root directory and add the structure for the program, including basic exception handling. |
| 42 | + |
| 43 | + ```python |
| 44 | + import os |
| 45 | + from azure.communication.email import EmailClient |
| 46 | + |
| 47 | + try: |
| 48 | + # Quickstart code goes here. |
| 49 | + except Exception as ex: |
| 50 | + print('Exception:') |
| 51 | + print(ex) |
| 52 | + ``` |
| 53 | + |
| 54 | +In the following sections, you'll add all the source code for this quickstart to the **send-email.py** file that you just created. |
| 55 | + |
| 56 | +### Install the package |
| 57 | + |
| 58 | +While still in the application directory, install the Azure Communication Services Email SDK for Python package by using the following command. |
| 59 | + |
| 60 | +```console |
| 61 | +pip install azure-communication-email |
| 62 | +``` |
| 63 | + |
| 64 | +## Object model |
| 65 | + |
| 66 | +The following classes and interfaces handle some of the major features of the Azure Communication Services SMS SDK for Python. |
| 67 | + |
| 68 | +| Name | Description | |
| 69 | +| ---- |-------------| |
| 70 | +| EmailAddress | This interface contains an email address and an option for a display name. | |
| 71 | +| EmailAttachment | This interface creates an email attachment by accepting a unique ID, email attachment type, and a string of content bytes. | |
| 72 | +| EmailClient | This class is needed for all email functionality. You instantiate it with your connection string and use it to send email messages. | |
| 73 | +| EmailClientOptions | This interface can be added to the EmailClient instantiation to target a specific API version. | |
| 74 | +| EmailContent | This interface contains the subject, plaintext, and html of the email message. | |
| 75 | +| EmailCustomHeader | This interface allows for the addition of a name and value pair for a custom header. | |
| 76 | +| EmailMessage | This interface combines the sender, content, and recipients. Custom headers, importance, attachments, and reply-to email addresses can optionally be added as well. | |
| 77 | +| EmailRecipients | This interface holds lists of EmailAddress objects for recipients of the email message, including optional lists for CC & BCC recipients. | |
| 78 | +| SendStatusResult | This interface holds the messageId and status of the email message delivery. | |
| 79 | + |
| 80 | +## Authenticate the client |
| 81 | + |
| 82 | +Instantiate an **EmailClient** with your connection string. Learn how to [manage your resource's connection string](../../create-communication-resource.md#store-your-connection-string). |
| 83 | + |
| 84 | +```python |
| 85 | +# Create the EmailClient object that you use to send Email messages. |
| 86 | +email_client = EmailClient.from_connection_string(<connection_string>) |
| 87 | +``` |
| 88 | + |
| 89 | +For simplicity, this quickstart uses connection strings, but in production environments, we recommend using [service principals](../../../quickstarts/identity/service-principal.md). |
| 90 | + |
| 91 | +## Send an email message |
| 92 | + |
| 93 | +To send an email message, you need to |
| 94 | +- Construct the EmailContent |
| 95 | +- Create an EmailAddress for the recipient |
| 96 | +- Construct the EmailRecipients |
| 97 | +- Construct the EmailMessage with the EmailContent, EmailAddress, and the sender information from the MailFrom address of your verified domain |
| 98 | +- Call the send method |
| 99 | + |
| 100 | +```python |
| 101 | +content = EmailContent( |
| 102 | + subject="Welcome to Azure Communication Services Email", |
| 103 | + plain_text="This email message is sent from Azure Communication Services Email using the Python SDK.", |
| 104 | +) |
| 105 | + |
| 106 | +address = EmailAddress( email="<[email protected]>") |
| 107 | +recipient = EmailRecipients(to=[address]) |
| 108 | + |
| 109 | +message = EmailMessage( |
| 110 | + |
| 111 | + content=content, |
| 112 | + recipients=recipients |
| 113 | + ) |
| 114 | + |
| 115 | +response = email_client.send(message) |
| 116 | +``` |
| 117 | + |
| 118 | +Make these replacements in the code: |
| 119 | + |
| 120 | +- Replace `<[email protected]>` with the email address you would like to send a message to. |
| 121 | +- Replace `<[email protected]>` with the MailFrom address of your verified domain. |
| 122 | + |
| 123 | +## Retrieve the Message ID of the email delivery |
| 124 | + |
| 125 | +To track the status of the email delivery, you will need the `message_id` from the response. |
| 126 | + |
| 127 | +```python |
| 128 | +message_id = response.message_id |
| 129 | +``` |
| 130 | + |
| 131 | +## Get the status of the email delivery |
| 132 | + |
| 133 | +We can keep checking the email delivery status until the status is `OutForDelivery`. |
| 134 | + |
| 135 | +```python |
| 136 | +counter = 0 |
| 137 | +while True: |
| 138 | + counter+=1 |
| 139 | + send_status = client.get_send_status(message_id) |
| 140 | + |
| 141 | + if (send_status): |
| 142 | + print(f"Email status for message_id {message_id} is {send_status.status}.") |
| 143 | + if (send_status.status.lower() == "queued" and counter < 12): |
| 144 | + time.sleep(10) # wait for 10 seconds before checking next time. |
| 145 | + counter +=1 |
| 146 | + else: |
| 147 | + if(send_status.status.lower() == "outfordelivery"): |
| 148 | + print(f"Email delivered for message_id {message_id}.") |
| 149 | + break |
| 150 | + else: |
| 151 | + print("Looks like we timed out for checking email send status.") |
| 152 | + break |
| 153 | +``` |
| 154 | + |
| 155 | +| Status Name | Description | |
| 156 | +| ----------- | ------------| |
| 157 | +| Queued | The email has been placed in the queue for delivery. | |
| 158 | +| OutForDelivery | The email is currently en route to its recipient(s). | |
| 159 | +| Dropped | The email message was dropped before the delivery could be successfully completed. | |
| 160 | + |
| 161 | +## Run the code |
| 162 | + |
| 163 | +Run the application from your application directory with the `python` command. |
| 164 | + |
| 165 | +```console |
| 166 | +python send-email.py |
| 167 | +``` |
| 168 | + |
| 169 | +## Sample code |
| 170 | + |
| 171 | +You can download the sample app from [GitHub](https://github.com/Azure-Samples/communication-services-python-quickstarts/tree/main/send-email) |
0 commit comments