0

I'm trying to send email using smtp.gmail.com in Django project. This is my email settings.

settings.py

# Email Settings

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'myaccount@gmail.com'
EMAIL_HOST_PASSWORD = 'mygooglepassword'

views.py

...
send_mail( "message title", 
           "message content", 
           "myaccount@gmail.com", 
           ["myaccount@hotmail.com"], 
           fail_silently=False)

Whenever I try to send email, I get this error

gaierror at /contact-us/

[Errno-2] Name or service not known

I tried the followings.

  1. I set my google account's less secure app access on.
  2. I unchecked avast antivirus setting 'Setting->Protection->Core Shields->Mail Shield->Scan outbound emails(SMTP)'
  3. Tried different ports in email settings. 587 and 25
  4. Switched the ssl and tls in email settings.

But it's not sending yet. When I use 'django.core.mail.backends.console.EmailBackend' instead of 'django.core.mail.backends.smtp.EmailBackend', it prints email on console.

I double checked my gmail username and password on settings. Please help me.

Thank you.

Community
  • 1
  • 1
Ma Long
  • 135
  • 2
  • 11

1 Answers1

0

You may need to do some configuration on Google side.

Reference answer::

Go to your Google Account settings, find Security -> Account permissions -> Access for less secure apps, enable this option.

https://accounts.google.com/DisplayUnlockCaptcha

Jun Zhou
  • 1,077
  • 1
  • 6
  • 19
  • Thank you for your advice. I believe your answer is another one step to success. But unfortunately, it gets the same error yet. – Ma Long Mar 07 '20 at 08:17
  • Is your email port opening and not blocked by any firewalls? – Jun Zhou Mar 07 '20 at 08:27
  • What a pity, I know it is a connection issue but I am not sure where is it if you have configured your Google account for insecure app. If I were you, I would create a new temp localhost Django project(not with ssl and any additional stuff) just with the email settings and test send_email functionality with existing settings. – Jun Zhou Mar 07 '20 at 08:43
  • 1
    The less secure app option is now disable by Google as of May 30, 2022... – Frederic Perron Jun 13 '22 at 14:19