Skip to main content

This is an alpha library to send email using Microsoft Graph API, a Microsoft 365 account is required to use this library

Project description

azure-simple-email

PyPI version License: MIT Python 3.10+

A lightweight Python library for sending email through Microsoft Azure using the Microsoft Graph API.

It wraps the OAuth2 client-credentials flow (via MSAL) and the Graph sendMail endpoint into a simple, reusable class — no boilerplate required.

Features

  • Plain-text and HTML emails
  • Multiple To / CC / BCC recipients
  • File attachments (from disk or in-memory bytes)
  • Reply-To header support
  • Token caching — send multiple emails without re-authenticating
  • Debug mode to redirect all outgoing mail to a test address

Installation

pip install azure-simple-email

Quick start

from azure_simple_email import AzureSendMail

mailer = AzureSendMail(user_id="noreply@example.com")
mailer.add_recipient("alice@example.com")
mailer.send_email(subject="Hello", text="Hi Alice!")

Configuration

The library reads credentials from three environment variables:

Variable Description
AZURE_EMAIL_CLIENT_ID Azure AD application (client) ID
AZURE_EMAIL_CLIENT_SECRET Azure AD client secret
AZURE_EMAIL_CLIENT_AUTHORITY Authority URL, e.g. https://login.microsoftonline.com/<tenant-id>

You can set them in a .env file and load it with python-dotenv, or export them directly in your shell.

Setting up the Azure app registration

  1. Go to Azure portal → Azure Active Directory → App registrations → New registration.
  2. Under API permissions, add Microsoft Graph → Application permissions → Mail.Send.
  3. Click Grant admin consent.
  4. Under Certificates & secrets, create a client secret and copy its value.

Usage examples

HTML email with attachment

from azure_simple_email import AzureSendMail

mailer = AzureSendMail(user_id="noreply@example.com")
mailer.add_recipient("alice@example.com")
mailer.add_attachment("/path/to/report.pdf")
mailer.send_email(
    subject="Monthly report",
    text="<h1>Report</h1><p>Please find the report attached.</p>",
    content_type="HTML",
)

Multiple recipients with CC and BCC

mailer = AzureSendMail(user_id="noreply@example.com")
mailer.add_recipient("alice@example.com")
mailer.add_cc_recipient("bob@example.com")
mailer.add_bcc_recipient("audit@example.com")
mailer.send_email(subject="Update", text="Quarterly update.")

Sending multiple emails (token reuse)

mailer = AzureSendMail(user_id="noreply@example.com")

for address in ["alice@example.com", "bob@example.com"]:
    mailer.add_recipient(address)
    mailer.send_email(subject="Newsletter", text="Hello!")
    mailer.clear()  # resets recipients/attachments, keeps the token

See the full documentation and the examples/ directory for more.

Building the documentation locally

pip install -r docs/requirements.txt
sphinx-build -b html docs/ docs/_build/html

Then open docs/_build/html/index.html in your browser.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

azure_simple_email-0.3.0.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

azure_simple_email-0.3.0-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file azure_simple_email-0.3.0.tar.gz.

File metadata

  • Download URL: azure_simple_email-0.3.0.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for azure_simple_email-0.3.0.tar.gz
Algorithm Hash digest
SHA256 02c616d620320edf43a8553c7ec09ab406cddbd4546c212cd059f8b447adbf16
MD5 93dc4b3a2c10d1b7e6b93dbdfb1c5ef8
BLAKE2b-256 cedbe5f1f8504a256709d152415480346671657deff4b5ec4ba96f3c4a9e1a2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for azure_simple_email-0.3.0.tar.gz:

Publisher: ci-cd.yml on marcotn/azure_simple_email

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file azure_simple_email-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for azure_simple_email-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca0b3f22d1eae8daaf1879dd524ca9d8ce1def21977fae7d8c899a1e5654d470
MD5 f3510fc112e2f6220b131efbe1f73cbb
BLAKE2b-256 64109c7aa29c6efc99aa1759f3798eca4ba31039848d81a98bb1ebcfa9d012db

See more details on using hashes here.

Provenance

The following attestation bundles were made for azure_simple_email-0.3.0-py3-none-any.whl:

Publisher: ci-cd.yml on marcotn/azure_simple_email

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page