1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
# coding: utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
FILE: send_email_to_multiple_recipient_sample.py
DESCRIPTION:
This sample demonstrates sending an email to multiple recipients. The Email client is
authenticated using a connection string.
USAGE:
python send_email_to_single_recipient_sample.py
Set the environment variable with your own value before running the sample:
1) COMMUNICATION_CONNECTION_STRING - the connection string in your ACS resource
2) SENDER_ADDRESS - the address found in the linked domain that will send the email
3) RECIPIENT_ADDRESS - the address that will receive the email
4) SECOND_RECIPIENT_ADDRESS - the second address that will receive the email
"""
import os
import sys
from azure.core.exceptions import HttpResponseError
from azure.communication.email import EmailClient
sys.path.append("..")
class EmailMultipleRecipientSample(object):
connection_string = os.getenv("COMMUNICATION_CONNECTION_STRING_EMAIL")
sender_address = os.getenv("SENDER_ADDRESS")
recipient_address = os.getenv("RECIPIENT_ADDRESS")
second_recipient_address = os.getenv("SECOND_RECIPIENT_ADDRESS")
def send_email_to_multiple_recipients(self):
# creating the email client
email_client = EmailClient.from_connection_string(self.connection_string)
# creating the email message
message = {
"content": {
"subject": "This is the subject",
"plainText": "This is the body",
"html": "html><h1>This is the body</h1></html>",
},
"recipients": {
"to": [
{"address": self.recipient_address, "displayName": "Customer Name"},
{"address": self.second_recipient_address, "displayName": "Customer Name 2"},
],
"cc": [
{"address": self.recipient_address, "displayName": "Customer Name"},
{"address": self.second_recipient_address, "displayName": "Customer Name 2"},
],
"bcc": [
{"address": self.recipient_address, "displayName": "Customer Name"},
{"address": self.second_recipient_address, "displayName": "Customer Name 2"},
],
},
"senderAddress": self.sender_address,
}
try:
# sending the email message
poller = email_client.begin_send(message)
response = poller.result()
print("Operation ID: " + response["id"])
except HttpResponseError as ex:
print(ex)
pass
if __name__ == "__main__":
sample = EmailMultipleRecipientSample()
sample.send_email_to_multiple_recipients()
|