Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 988
Rakib Avatar asked Feb 26 '16 18:02

Rakib


People also ask

How do I send and receive emails in Django?

Use the get_new_mail method to collect new messages from the server. Go to Django Admin, then to 'Mailboxes' page, check all the mailboxes you need to receive emails from. At the top of the list with mailboxes, choose the action selector 'Get new mail' and click 'Go'.


1 Answers

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?

like image 53
jape Avatar answered Sep 28 '22 06:09

jape