Skip to content

Email (SMTP)

Optional transactional email uses Django’s SMTP backend. Store SMTP settings as a JSON secret in the app’s Key Vault via the usual secrets= mapping (Secrets). Configuring MAILERS is left to the consumer project — it is not Azure-specific.

Any SMTP provider works. Amazon SES SMTP is a common choice and is fully compatible with these settings.

Pulumi config

Store SMTP options as a structured secret (not in Git). Keys should match Django SMTP OPTIONS:

pulumi config set --secret --path 'smtp.host' 'email-smtp.eu-central-1.amazonaws.com'
pulumi config set --secret --path 'smtp.username' '...'
pulumi config set --secret --path 'smtp.password' '...'
pulumi config set --secret --path 'smtp.use_tls' true

Use a per-site name if you want separate credentials, e.g. web_smtp instead of smtp.

Wire a website

django.add_django_website(
    name="web",
    # ...
    environment_variables={
        "DJANGO_DEFAULT_FROM_EMAIL": "noreply@example.com",
    },
    secrets={
        "smtp": "SMTP",
    },
)

That stores the config object in Key Vault and sets SMTP_SECRET_NAME on the Web App (same {ENV}_SECRET_NAME pattern as other secrets).

Configure Django in the consumer project

import json
import environ
from pulumi_django_azure.settings import AZURE_KEY_VAULT_CLIENT

env = environ.Env()

smtp_secret = AZURE_KEY_VAULT_CLIENT.get_secret(env("SMTP_SECRET_NAME"))
smtp_secret = json.loads(smtp_secret.value)

MAILERS = {
    "default": {
        "BACKEND": "pulumi_django_azure.mailers.MailerCredentialsSMTPBackend",
        "OPTIONS": smtp_secret,
    },
}

Set DJANGO_DEFAULT_FROM_EMAIL to an address your provider allows (typically via environment_variables).

Why MailerCredentialsSMTPBackend

pulumi_django_azure.mailers.MailerCredentialsSMTPBackend is a drop-in replacement for django.core.mail.backends.smtp.EmailBackend that keeps the credentials from OPTIONS when a caller passes None for them.

That is not hypothetical: the deprecated django.core.mail.get_connection() shim applies the arguments it is given on top of the mailer's OPTIONS (MailersHandler.create_connection() does options = options | _deprecated_kwargs), and Wagtail calls it that way in wagtail.admin.mail.send_mail(). On a site with SMTP credentials in MAILERS, form-page notification emails were therefore sent from an unauthenticated session, and the provider rejected them at MAIL FROM:

smtplib.SMTPSenderRefused: (530, b'Authentication required', 'website@example.com')

Credentials a caller passes explicitly still win, and mailers that hold no credentials (console, locmem, Mailpit) behave exactly as before. When the callers stop passing None, the standard backend can be used again.

Amazon SES (optional)

If you use Amazon SES:

  1. Verify each sending domain (SPF, DKIM, and optionally DMARC).
  2. Leave the SES sandbox (or request production access) for arbitrary recipients.
  3. Create SMTP credentials in the SES console for your region.
  4. Put the regional endpoint in smtp.host, e.g. email-smtp.eu-central-1.amazonaws.com.

You can reuse one SMTP config object across sites in the same stack when that fits your isolation model.

Test on Azure

With pulumi_django_azure in INSTALLED_APPS, SSH into the Web App (Azure Portal → SSH / az webapp ssh) and run:

python manage.py test_email you@example.com

Optional flags: --from, --using (mailer alias), --subject. The command uses DEFAULT_FROM_EMAIL when --from is omitted, and prints the mailer backend / non-secret OPTIONS before sending.

Local development

Leave SMTP_SECRET_NAME unset and configure your own MAILERS (console backend, Mailpit, etc.) in the consumer settings module.

DNS

Publish whatever SPF / DKIM / verification records your provider requires. That is outside this package — same as other public DNS work. See Domains and HTTPS for App Service / CDN DNS.