I'm trying to send an email from Django using Gmail SMTP

Question:

I’ve set insecure source ON in my account’s setting. Also, this is my settings.py file. Can anybody help me?

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com '
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER =' my email'
EMAIL_HOST_PASSWORD = 'my password'

and I’m getting this error in terminal when I write this code

PS D:start heresilicium7config-project> python manage.py sendtestemail [email protected] 
Traceback (most recent call last):
 File "manage.py", line 22, in <module>
   main()
 File "manage.py", line 18, in main
   execute_from_command_line(sys.argv)
 File "D:start heresilicium7libsite-packagesdjangocoremanagement__init__.py", line 401, in execute_from_command_line
   utility.execute()
 File "D:start heresilicium7libsite-packagesdjangocoremanagement__init__.py", line 395, in execute
   self.fetch_command(subcommand).run_from_argv(self.argv)
 File "D:start heresilicium7libsite-packagesdjangocoremanagementbase.py", line 330, in run_from_argv
   self.execute(*args, **cmd_options)
 File "D:start heresilicium7libsite-packagesdjangocoremanagementbase.py", line 371, in execute
   output = self.handle(*args, **options)
 File "D:start heresilicium7libsite-packagesdjangocoremanagementcommandssendtestemail.py", line 33, in handle
   recipient_list=kwargs['email'],
 File "D:start heresilicium7libsite-packagesdjangocoremail__init__.py", line 61, in send_mail
   return mail.send()
 File "D:start heresilicium7libsite-packagesdjangocoremailmessage.py", line 284, in send
   return self.get_connection(fail_silently).send_messages([self])
 File "D:start heresilicium7libsite-packagesdjangocoremailbackendssmtp.py", line 102, in send_messages
   new_conn_created = self.open()
 File "D:start heresilicium7libsite-packagesdjangocoremailbackendssmtp.py", line 62, in open
   self.connection = self.connection_class(self.host, self.port, **connection_params)
 File "C:UsersAta BarzegarAppDataLocalProgramsPythonPython37libsmtplib.py", line 251, in __init__
   (code, msg) = self.connect(host, port)
 File "C:UsersAta BarzegarAppDataLocalProgramsPythonPython37libsmtplib.py", line 336, in connect
   self.sock = self._get_socket(host, port, self.timeout)
 File "C:UsersAta BarzegarAppDataLocalProgramsPythonPython37libsmtplib.py", line 307, in _get_socket
   self.source_address)
   for res in getaddrinfo(host, port, 0, SOCK_STREAM):
 File "C:UsersAta BarzegarAppDataLocalProgramsPythonPython37libsocket.py", line 752, in getaddrinfo
   for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
Asked By: Ata barzegar

||

Answers:

This is may be useful for you.

Add DEFAULT_FROM_EMAIL in settings.py and test it through python shell:

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

DEFAULT_FROM_EMAIL = '[email protected]'

Run python shell:

python manage.py shell
from django.conf import settings
from django.core.mail import send_mail

subject = 'Some subject'
from_email = settings.DEFAULT_FROM_EMAIL   
message = 'This is my test message'
recipient_list = ['[email protected]','[email protected]']           
html_message = '<h1>This is my HTML test</h1>'
send_mail(subject, message, from_email, recipient_list, fail_silently=False, html_message=html_message)

If output code is 1 then email sent successfully….

Answered By: Pradip
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.