API Guide

Model Class

class azmailer.azmailer.AzMailer(connection_string: str, sender_address: str)

Class for sending emails with Azure Communication Services.

Inspired by the documentation: https://learn.microsoft.com/en-us/azure/communication-services/quickstarts/email/send-email?tabs=linux%2Cconnection-string&pivots=programming-language-python

Usage:

from azmailer import AzMailer
mailer = AzMailer(os.environ["AZURE_COMMUNICATION_CONNECTION_STRING"], os.environ["AZURE_COMMUNICATION_SENDER_ADDRESS"])
email_message = mailer.construct_message(
    subject="Attached code",
    content="<html><body><h1>A code!</h1>to control them all!</body></html>",
    # content="a code to control them all!",
    to_addresses=["marcanglisano@gmail.com"],
    attachments=["path/to/file1","path/to/file2"]
)
mailer.send_email(email_message)
construct_message(subject: str, content: str, to_addresses: list, cc_addresses: list = None, bcc_addresses: list = None, attachments: list | str = None, user_engagement_tracking_disabled: bool = False) dict

Construct an email message.

Args:

subject (str): Email subject. content (str): Email content (HTML or plain text). to_addresses (list): List of recipient email addresses. cc_addresses (list, optional): List of CC email addresses. Defaults to None. bcc_addresses (list, optional): List of BCC email addresses. Defaults to None. attachments (Union[list, str], optional): List of file paths or a single file path to attach. Defaults to None. user_engagement_tracking_disabled (bool, optional): Disable user engagement tracking. Defaults to False.

Returns:

dict: Constructed email message dictionary.

send_email(message: dict)

Send an email.

Args:

message (dict): Email message dictionary.

Raises:

RuntimeError: If sending the email fails.