Ata barzegar
Ata barzegar

Reputation: 117

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

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 here\silicium7\config-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 here\silicium7\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
   utility.execute()
 File "D:\start here\silicium7\lib\site-packages\django\core\management\__init__.py", line 395, in execute
   self.fetch_command(subcommand).run_from_argv(self.argv)
 File "D:\start here\silicium7\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
   self.execute(*args, **cmd_options)
 File "D:\start here\silicium7\lib\site-packages\django\core\management\base.py", line 371, in execute
   output = self.handle(*args, **options)
 File "D:\start here\silicium7\lib\site-packages\django\core\management\commands\sendtestemail.py", line 33, in handle
   recipient_list=kwargs['email'],
 File "D:\start here\silicium7\lib\site-packages\django\core\mail\__init__.py", line 61, in send_mail
   return mail.send()
 File "D:\start here\silicium7\lib\site-packages\django\core\mail\message.py", line 284, in send
   return self.get_connection(fail_silently).send_messages([self])
 File "D:\start here\silicium7\lib\site-packages\django\core\mail\backends\smtp.py", line 102, in send_messages
   new_conn_created = self.open()
 File "D:\start here\silicium7\lib\site-packages\django\core\mail\backends\smtp.py", line 62, in open
   self.connection = self.connection_class(self.host, self.port, **connection_params)
 File "C:\Users\Ata Barzegar\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 251, in __init__
   (code, msg) = self.connect(host, port)
 File "C:\Users\Ata Barzegar\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 336, in connect
   self.sock = self._get_socket(host, port, self.timeout)
 File "C:\Users\Ata Barzegar\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 307, in _get_socket
   self.source_address)
   for res in getaddrinfo(host, port, 0, SOCK_STREAM):
 File "C:\Users\Ata Barzegar\AppData\Local\Programs\Python\Python37\lib\socket.py", line 752, in getaddrinfo
   for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

Upvotes: 2

Views: 5896

Answers (1)

Pradip Kachhadiya
Pradip Kachhadiya

Reputation: 2235

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....

Upvotes: 2

Related Questions