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

2

5

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

Wojcik answered 26/2, 2016 at 18:32 Comment(3)
You might need to unlock Captcha to enable Django to send for you: accounts.google.com/displayunlockcaptchaIncendiary
ok... i will try that... but how does it explain working fine from my localhost and working from EC2?Wojcik
@jape... it actually worked. Please write a proper StackOverflow answer with this so that i can accept. However, i still don't understand why was it working fine from localhost and not from the EC2 until i went to the link you sharedWojcik
I
16

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?

Incendiary answered 27/2, 2016 at 17:8 Comment(3)
This solution actually worked. But, how does google evaluate that the requests from localhost are by me and requests from EC2 are not by me? I don't get it.Wojcik
@syedrakib The captcha is there to detect if new devices are trying to access your account. I'm assuming you've gone onto your email from your computer before (localhost). Google has given your machine the green light. This is probably the first time Amazon is accessing it. You needed to tell Google to allow Amazon to get in.Incendiary
@syedrakib I'm glad! Good luck :)Incendiary
C
1

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.

Castellated answered 26/2, 2016 at 20:56 Comment(3)
are you serious? AWS will block me trying to send email via django but not block me when using mailgun?Wojcik
Yep, because mailgun uses a REST API instead of SMTP.Castellated
It also appears you can use SMTP if you use non-default ports.Castellated

© 2022 - 2024 — McMap. All rights reserved.