Send transactional and bulk email from Python with the Elastic Email email API. Standalone scripts plus three web applications - Flask, FastAPI and Django - built on the official Elastic Email Python SDK.
First time here? The Python quickstart gets you from nothing to a delivered email in five minutes. For the concepts behind these examples, see the Elastic Email guides.
Versions: Elastic Email REST API v4 · SDK ElasticEmail==4.4.0 (PyPI) · Python 3.9+ · Flask, FastAPI, Django
- Python 3.9+
- pip
- An Elastic Email account with a verified sender domain (How to verify your domain)
- API key from app.elasticemail.com (API settings)
# Create virtual environment (optional)
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Copy environment variables
cp .env.example .env
# Add your Elastic Email API key to .envRun every script from this folder.
python examples/basic_send.pypython examples/batch_send.pypython examples/with_attachments.pypython examples/with_cid_attachments.pypython examples/with_template.pypython examples/scheduled_send.pypython examples/prevent_threading.pypython examples/contacts.pypython examples/domains.pypython examples/email_status.py <transactionId> [messageId]python examples/webhooks.pyCreates a signed webhook (EnableSigning) that POSTs JSON (DeliveryMethod: PostJson), prints its SigningSecret, rotates the secret, then deletes it. Store the secret as ELASTICEMAIL_WEBHOOK_SECRET: it is shown only once. With it set, the webhook routes require a valid X-ElasticEmail-Signature (HMAC-SHA256 over timestamp, method, raw request target and raw body) and answer 401 otherwise. Leave it empty to use unsigned webhooks with ?token=ELASTICEMAIL_WEBHOOK_TOKEN in the URL instead.
python examples/inbound.py# Subscribe (creates contact + sends confirmation)
python examples/double_optin_subscribe.py user@example.com "John Doe"
# Click-tracking based confirmation server
python examples/double_optin_webhook.pypython examples/suppressions.py [email]python examples/email_verification.py someone@example.compython examples/statistics.pypython examples/sub_accounts.py
# Creating a sub-account affects billing:
CREATE_SUBACCOUNT=true python examples/sub_accounts.pypython examples/flask_app.py
# Then in another terminal:
curl -X POST http://localhost:3000/send \
-H "Content-Type: application/json" \
-d '{"to": "you@yourdomain.com", "subject": "Hello", "message": "Hi from Flask!"}'python examples/fastapi_app.py
# or: uvicorn examples.fastapi_app:app --reload --port 3000
curl -X POST http://localhost:3000/send \
-H "Content-Type: application/json" \
-d '{"to": "you@yourdomain.com", "subject": "Hello", "message": "Hi from FastAPI!"}'cd django_app
python manage.py runserver 8001
curl -X POST http://localhost:8001/send \
-H "Content-Type: application/json" \
-d '{"to": "you@yourdomain.com", "subject": "Hello", "message": "Hi from Django!"}'All three server apps expose the same routes and JSON shapes.
GET /health->{"status": "ok"}POST /sendbody{"to", "subject", "message"}->{"success": true, "transactionId", "messageId"}; an optionalIdempotency-Keyrequest header is forwarded to Elastic Email (a new key is generated otherwise), so clients can retry safely with the same keyGET|POST /webhook- Elastic Email event notifications (status,to,transaction,messageid,target, ...). Recommended: a signed webhook withDeliveryMethod: PostJson, which sends each event as a POST with a JSON body; setELASTICEMAIL_WEBHOOK_SECRETand the route verifies the signature (401if it is missing or wrong). Fallback for unsigned webhooks: leave the secret empty, add?token=...matchingELASTICEMAIL_WEBHOOK_TOKENto the URL, and the details arrive in the query string of a GET.POST /inbound?token=...- inbound email pushed by an inbound route (from_email,subject,body_html,att1_name,att1_content, ...). Forwards a copy toCONTACT_EMAIL.POST /double-optin/subscribebody{"email", "name"}- stores the contact as Transactional and sends a confirmation linkGET /double-optin/confirm?email=&token=- verifies the HMAC token and adds the contact toELASTICEMAIL_LIST_NAMEGET|POST /double-optin/webhook- confirms on aClickedevent whosetargetis the confirm link (signed, or?token=...like/webhook)
Failures return {"error": "<message from the API>"} with the API status code.
import ElasticEmail
configuration = ElasticEmail.Configuration()
configuration.api_key["apikey"] = "your_api_key"
with ElasticEmail.ApiClient(configuration) as api_client:
emails_api = ElasticEmail.EmailsApi(api_client)
result = emails_api.emails_transactional_post(
ElasticEmail.EmailTransactionalMessageData(
Recipients=ElasticEmail.TransactionalRecipient(To=["you@yourdomain.com"]),
Content=ElasticEmail.EmailContent(
From="Acme <hello@yourdomain.com>",
Subject="Hello",
Body=[ElasticEmail.BodyPart(ContentType="HTML", Content="<p>Hello World</p>")],
),
)
)
print(result.transaction_id, result.message_id)python-elasticemail-examples/
├── examples/
│ ├── ee.py # Shared config (API key, env values)
│ ├── basic_send.py # Simple transactional email
│ ├── batch_send.py # Bulk send with merge fields
│ ├── with_attachments.py # Emails with files
│ ├── with_cid_attachments.py # Inline images
│ ├── with_template.py # Templates with merge values
│ ├── scheduled_send.py # Delayed delivery (TimeOffset)
│ ├── prevent_threading.py # Prevent Gmail threading
│ ├── contacts.py # Contacts and lists
│ ├── domains.py # Domain verification
│ ├── email_status.py # Delivery status by transaction id
│ ├── webhooks.py # Manage webhooks
│ ├── inbound.py # Manage inbound routes
│ ├── double_optin_subscribe.py # Double opt-in: subscribe
│ ├── double_optin_webhook.py # Double opt-in: click-based confirm server
│ ├── suppressions.py # Unsubscribes, bounces, complaints
│ ├── email_verification.py # Verify an address
│ ├── statistics.py # Account statistics
│ ├── sub_accounts.py # Sub-accounts (read-only by default)
│ ├── flask_app.py # Flask web application
│ └── fastapi_app.py # FastAPI web application
├── django_app/ # Django web application
│ ├── manage.py
│ ├── django_project/
│ └── elasticemail_app/
├── requirements.txt
├── .env.example
└── README.md
- Elastic Email email API - REST API and SMTP relay overview, features and plans
- Elastic Email Python SDK
- Elastic Email API Reference
- Elastic Email Developers
MIT