Hello!
Have anyone managed to integrate Django with Anymail?
I have spent 2 days figuring out what I am doing wrong, with some food and gym pauses in between. But I am stuck in a big whole I believe.
I am trying to use Anymail to send emails via Mailgun in my project, but I get this error for some reason:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/app/.heroku/python/lib/python3.12/site-packages/django/core/mail/__init__.py", line 88, in send_mail
return mail.send()
^^^^^^^^^^^
File "/app/.heroku/python/lib/python3.12/site-packages/django/core/mail/message.py", line 301, in send
return self.get_connection(fail_silently).send_messages([self])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.heroku/python/lib/python3.12/site-packages/anymail/backends/base.py", line 117, in send_messages
sent = self._send(message)
^^^^^^^^^^^^^^^^^^^
File "/app/.heroku/python/lib/python3.12/site-packages/anymail/backends/base_requests.py", line 51, in _send
return super()._send(message)
^^^^^^^^^^^^^^^^^^^^^^
File "/app/.heroku/python/lib/python3.12/site-packages/anymail/backends/base.py", line 147, in _send
response = self.post_to_esp(payload, message)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.heroku/python/lib/python3.12/site-packages/anymail/backends/base_requests.py", line 108, in post_to_esp
self.raise_for_status(response, payload, message)
File "/app/.heroku/python/lib/python3.12/site-packages/anymail/backends/mailgun.py", line 59, in raise_for_status
super().raise_for_status(response, payload, message)
File "/app/.heroku/python/lib/python3.12/site-packages/anymail/backends/base_requests.py", line 119, in raise_for_status
raise AnymailRequestsAPIError(
anymail.exceptions.AnymailRequestsAPIError: Mailgun API response 401 (Unauthorized): 'Forbidden'
So the issue seem to be with the Mailgun API not responding.
What I have done so far is:
- Verified that the API key is correct (from Mailgun → Domain settings → Sending API keys).
- I have created a config variable in Heroku with the API key.
- Tested my email settings with python manage.py sendtestmail (everything was fine)
- I have configured my DNS settings. There are some green marks in Mailgun → DNS records
- I have a paid Mailgun plan. The free one does not allow emails from unverified emails.
And a lot more.
This is my production settings:
# Email back-end
EMAIL_BACKEND = 'anymail.backends.mailgun.EmailBackend'
ANYMAIL = {
'MAILGUN_API_KEY': os.environ.get("MAILGUN_API_KEY"),
'MAILGUN_SENDER_DOMAIN': os.environ.get("MAILGUN_DOMAIN"),
}
# SMTP settings
# EMAIL_HOST = os.getenv("MAILGUN_SMTP_SERVER")
# EMAIL_PORT = os.getenv("MAILGUN_SMTP_PORT")
# EMAIL_HOST_USER = os.getenv("MAILGUN_SMTP_LOGIN") # Email user
# EMAIL_HOST_PASSWORD = os.getenv("MAILGUN_SMTP_PASSWORD") # Email password
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = os.environ.get("DEFAULT_FROM_EMAIL")
SERVER_EMAIL = os.environ.get("SERVER_EMAIL")
And I import the the variables from my .env file.
# Email settings
MAILGUN_API_KEY = "XXXXXXXXXXXXXXX-XXXXXXXXXXXXXXX"
MAILGUN_SENDER_DOMAIN = "XXXXX.com"
MAILGUN_PUBLIC_KEY = "pubkey-XXXXXXXXXXXXXXX"
# SMTP settings
MAILGUN_SMTP_LOGIN = "postmaster@XXXX.com"
MAILGUN_SMTP_PASSWORD = "XXXXXXXXXXXXXXX-XXXXXXXX-XXXXXXXX"
MAILGUN_SMTP_PORT = 587
MAILGUN_SMTP_SERVER = "smtp.eu.mailgun.org"
MAILGUN_API_URL = "https://api.eu.mailgun.net/v3"
ADMIN_EMAIL_ADDRESS = "XXXXXXXX@outlook.com"
DEFAULT_FROM_EMAIL = "XXXXXXXX@outlook.com"
SERVER_EMAIL = "XXXXXXXX@outlook.com"
And finally, ‘anymail’ is included in my installed apps list.
Do anyone know what I am doing wrong or can suggest a possible solution I can try?