Django send_mail() from EC2 via Gmail gives SMTPAuthenticationError - but works fine in localhost

2.6k Views Asked by At

Django project settings.py includes the following:

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_USER = "[email protected]"
EMAIL_HOST_PASSWORD = "thug_life"
EMAIL_PORT = 587
EMAIL_USE_TLS = True

My Application's views.py contains the following

def send_classic_email(request):
    from django.core.mail import send_mail
    send_mail(
        subject = "Tale of two cities",
        from_email = "Charles Dickens <[email protected]>",
        recipient_list = ["[email protected]"],
        message = "There were 2 cities",
        html_message = "<p>There were 2 cities</p>",
        fail_silently = False,
    )
    print "Absolutely Perfectly Done"

Tried from localhost. Got SMTPAuthenticationError in return:

SMTPAuthenticationError at /send_classic_email/
(534, '1.3.95 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=fsadjSADJH\n1.3.95 
fjkshFHAKSHkdfshkfkhj-sfjdhFsadASDA_\n1.3.95 
dasdASDADas-aDas-hfhjsadASDSAhjjhd\n1.3.95 
ADSaSADkja_adhjkADKjhads-ASADS_SDAKjadAKJhsADS-k\n1.3.95 
sadhkjADSAKJSDJAlkjdaA> Please log in via your web browser and\n1.3.95 
then try again.\n1.3.95  
Learn more at\n1.3.95  
https://support.google.com/mail/answer/78754 dkahASDASlkjdas.25 - gsmtp')

Then visited https://www.google.com/settings/security/lesssecureapps and enabled the less secure app setting.

After that, tried once again from localhost. Got this:

Absolutely Perfectly Done

Deployed this very code on AWS EC2. Tried from EC2. Got the same SMTPAuthenticationError again:

SMTPAuthenticationError at /send_classic_email/
(534, '1.3.95 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=fsadjSADJH\n1.3.95 
fjkshFHAKSHkdfshkfkhj-sfjdhFsadASDA_\n1.3.95 
dasdASDADas-aDas-hfhjsadASDSAhjjhd\n1.3.95 
ADSaSADkja_adhjkADKjhads-ASADS_SDAKjadAKJhsADS-k\n1.3.95 
sadhkjADSAKJSDJAlkjdaA> Please log in via your web browser and\n1.3.95 
then try again.\n1.3.95  
Learn more at\n1.3.95  
https://support.google.com/mail/answer/78754 dkahASDASlkjdas.25 - gsmtp')

Went to EC2 security groups:

  • Inbound rules for SMTP port from ALL sources are enabled.
  • Outbound rules for ALL traffic for ALL ports over ALL protocols to ALL destinations are enabled.

Still getting the same SMTPAuthenticationError.

Why is it working fine from localhost and not from EC2 instance?

Running Django 1.8.0 on Python 2.7.6 in Ubuntu 14.04.3 LTS

2

There are 2 best solutions below

4
On BEST ANSWER

You probably need to unlock Captcha to enable Django to send for you: accounts.google.com/displayunlockcaptcha

Captcha are the little characters you need to type into a form to go to the next page. It's a security precaution most companies rely on.

The reason you are able to get away with it on your local host is because you are essentially the company controlling the captcha. You're telling your server, "send no matter what; it's safe." However, in this instance, Google is in control of the captcha. Since you're using Amazon, preventing the email is a way for Google to protect its servers and make sure Amazon isn't spam. By clicking the link, you are telling Google to allow all outgoing connections to happen from your email.

Does that make sense?

3
On

I have a feeling Amazon don't allow this. They really don't want you sending out mail from their servers, so they block it. It's to avoid spammers, basically, who used to send email from EC2 by the virtual truckload. I suggest that you use something like mailgun (or SES if you really love AWS, but I probably wouldn't) to handle sending email instead. There is a nice django email backend for mailgun, so you don't need to change your code at all.