Search code examples
djangomailgunpythonanywhere

Mailgun 404 error with Pythonanywhere


I'm trying to use mailgun in my Django project hosted on Pythonanywhere.

In my WSGI file, I have:

os.environ['DJANGO_MAILGUN_SERVER_NAME'] = 'https://api.mailgun.net/v3/sandboxnumbersomething.mailgun.org/messages'
os.environ['DJANGO_MAILGUN_API_KEY'] ='mykey'

and my settings are:

# EMAIL
# ------------------------------------------------------------------------------
DEFAULT_FROM_EMAIL = env('DJANGO_DEFAULT_FROM_EMAIL',
                         default='Apitrak <noreply@apitrak.com>')
EMAIL_BACKEND = 'django_mailgun.MailgunBackend'
MAILGUN_ACCESS_KEY = env('DJANGO_MAILGUN_API_KEY')
MAILGUN_SERVER_NAME = env('DJANGO_MAILGUN_SERVER_NAME')

When my app fire an email (for example at signup), I have a 404 error:

MailgunAPIError at /accounts/email/

<Response [404]>

Request Method:     POST
Request URL:    https://vincentle.pythonanywhere.com/accounts/email/
Django Version:     1.8.6
Exception Type:     MailgunAPIError
Exception Value:    

<Response [404]>

Exception Location:     /home/vincentle/.virtualenvs/apitrak/lib/python3.4/site-packages/django_mailgun.py in _send, line 154
Python Executable:  /usr/local/bin/uwsgi
Python Version:     3.4.0
Python Path:    

['/var/www',
 '.',
 '',
 '/var/www',
 '/home/vincentle/.virtualenvs/apitrak/lib/python3.4',
 '/home/vincentle/.virtualenvs/apitrak/lib/python3.4/plat-x86_64-linux-gnu',
 '/home/vincentle/.virtualenvs/apitrak/lib/python3.4/lib-dynload',
 '/usr/lib/python3.4',
 '/usr/lib/python3.4/plat-x86_64-linux-gnu',
 '/home/vincentle/.virtualenvs/apitrak/lib/python3.4/site-packages',
 '/home/vincentle/apitrak']

Server time:    Tue, 17 Nov 2015 16:02:28 +0100

I've tried a curl in the virtualenv of my WebApp:

curl -s --user 'api:key-NUMBERS' https://api.mailgun.net/v3/NUMBERS.mailgun.org/messages -F from='Excited User <excited@samples.mailgun.org>' -F to='vincent@vincentle.fr'  -F subject='Hello'  -F text='Testing some Mailgun awesomeness!'

And this works OK.


Solution

  • The setting DJANGO_MAILGUN_SERVER_NAME should be a domain name, not a url.

    Try the following:

    os.environ['DJANGO_MAILGUN_SERVER_NAME'] = '<sandboxnumbersomething>.mailgun.org'
    

    From the readme:

    Replace SERVER-NAME with the last part of your "API Base URL" (e.g. https://api.mailgun.net/v3/<your_server_name>), also found in your Mailgun account details.