django-anymail

14.0 · active · verified Sat Apr 11

Django Anymail provides a unified interface for sending transactional emails and receiving webhooks across many Email Service Providers (ESPs) like SendGrid, Mailgun, Postmark, and more. It integrates seamlessly with Django's `EmailBackend` and `send_mail` functions, adding advanced features like ESP-specific options, transactional tracking, and inbound email signals. The library is actively maintained with frequent releases, currently at version 14.0, and supports various Django and Python versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure and use django-anymail to send emails via an ESP (Mailgun is used as an example). It shows both the standard `send_mail` function and the extended `AnymailMessage` for ESP-specific features like tags. Remember to replace placeholder API keys and recipient emails. For a real Django project, the `settings.configure` block is not needed, as settings would be in your project's `settings.py`.

import os
from django.conf import settings
from django.core.mail import send_mail
from anymail.message import AnymailMessage

# --- Minimal Django settings configuration for standalone execution ---
# In a real Django project, these settings would be in your settings.py file.
# This block ensures the script can run without a full Django project setup.
if not settings.configured:
    settings.configure(
        ANYMAIL={
            "MAILGUN_API_KEY": os.environ.get("ANYMAIL_MAILGUN_API_KEY", "your-mailgun-api-key"),
            "MAILGUN_SENDER_DOMAIN": os.environ.get("ANYMAIL_MAILGUN_SENDER_DOMAIN", "mg.example.com"),
            # For other ESPs, replace MAILGUN_API_KEY/SENDER_DOMAIN with appropriate keys/settings
        },
        EMAIL_BACKEND="anymail.backends.mailgun.EmailBackend", # Or your chosen ESP backend
        DEFAULT_FROM_EMAIL="sender@example.com",
        DEBUG=True, # Minimal required setting for settings.configure
    )

print("Attempting to send an email using Django's send_mail...")

try:
    # Example 1: Basic email using Django's send_mail
    send_mail(
        subject="Hello from django-anymail!",
        message="This is a test email sent via Mailgun through django-anymail.",
        from_email=settings.DEFAULT_FROM_EMAIL,
        recipient_list=["recipient@example.com"], # Replace with a valid recipient email
        fail_silently=False,
    )
    print("Basic email send attempt successful.")

    # Example 2: Using AnymailMessage for ESP-specific features (e.g., tags)
    message = AnymailMessage(
        subject="AnymailMessage with Tags",
        body="This email demonstrates using AnymailMessage with custom tags.",
        from_email=settings.DEFAULT_FROM_EMAIL,
        to=["recipient2@example.com"], # Replace with another valid recipient email
    )
    message.tags = ["transactional", "test-tag"]
    # You can also add merge_data, metadata, etc.
    message.send(fail_silently=False)
    print("AnymailMessage with tags send attempt successful.")

except Exception as e:
    print(f"\nFailed to send email: {e}")
    print("Please ensure your environment variables (e.g., ANYMAIL_MAILGUN_API_KEY, ANYMAIL_MAILGUN_SENDER_DOMAIN) ")
    print("are correctly set and your ESP configuration in settings.py is valid.")
    print("Note: In a real Django project, you'd only need the settings in settings.py ")
    print("and directly call send_mail or AnymailMessage.send().")

view raw JSON →