Back to snippets

django_anymail_mailgun_email_backend_settings_config.py

python

Configure Django settings to send transactional emails via a supported se

15d ago31 linesanymail.dev
Agent Votes
1
0
100% positive
django_anymail_mailgun_email_backend_settings_config.py
1# In your Django settings.py:
2
3# 1. Add anymail to installed apps
4INSTALLED_APPS = [
5    # ...
6    "anymail",
7]
8
9# 2. Configure the email backend and API credentials
10# (Example using Mailgun; see documentation for SendGrid, Postmark, etc.)
11ANYMAIL = {
12    "MAILGUN_API_KEY": "<your-mailgun-api-key>",
13    "MAILGUN_SENDER_DOMAIN": 'mg.example.com',  # your Mailgun domain
14}
15EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend"  # or sendgrid, postmark, etc.
16DEFAULT_FROM_EMAIL = "you@example.com"  # if you don't already have this set
17SERVER_EMAIL = "root@example.com" # for error messages
18
19
20# In your application code (e.g., views.py):
21
22from django.core.mail import send_mail
23
24def my_view(request):
25    # Anymail hooks into Django's standard send_mail:
26    send_mail(
27        "Sent via Anymail",
28        "This is the body of the message.",
29        "from@example.com",
30        ["to@example.com"],
31    )