3

I searched a lot but didn't find a proper solution. Seems like nobody had this problem before.

Note my socket.gaierror is [Errno 10047] which means "Address family not supported by protocol family"

The error I'm getting is when I try to integrate SMTP in my project so I can send mails.

  File "C:\Users\Ubaid Parveez\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Users\Ubaid Parveez\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 307, in _get_socket
    self.source_address)
  File "C:\Users\Ubaid Parveez\AppData\Local\Programs\Python\Python36\lib\socket.py", line 705, in create_connection
    for res in getaddrinfo(host, port, 1, SOCK_STREAM):
  File "C:\Users\Ubaid Parveez\AppData\Local\Programs\Python\Python36\lib\socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 10047] getaddrinfo failed

Here's my settings for the smtp

SERVER_EMAIL = 'abc@example.com'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = 'password_here'
EMAIL_HOST_USER = SERVER_EMAIL
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

Here's my view which sends the email

class ContactView(generic.TemplateView):
    template_name = "contact.html"

    def post(self,request,*args,**kwargs):
       send_mail(
         'Subject here',
         'Here is the message.',
         EMAIL_HOST_USER,
         ['to@example.com'],
         fail_silently=False,
      )

And when I use the send_mail() function in my view. I get this socket error. This error goes away if I change my email backend to "django.core.mail.backends.console.EmailBackend".

  • can you please provide the code you are working with. Please reference https://stackoverflow.com/help/how-to-ask – James Powis Apr 18 '20 at 04:41
  • Hey I have provided the code that i' am working with.Lemme show you my view that sends the email too. – Ubaid Parvaiiz Apr 18 '20 at 04:42
  • You provided a stack trace and variables. That is not code. – James Powis Apr 18 '20 at 04:44
  • these variables were most import part in assuring that the code is working properlyAnyways I have provide the view too which uses django send_mail. – Ubaid Parvaiiz Apr 18 '20 at 04:48
  • 1
    what function or library is providing send_mail – James Powis Apr 18 '20 at 04:49
  • Basically django provides that functionality.And I know the error is not in send_mail but something is wrong with my machine or something else – Ubaid Parvaiiz Apr 18 '20 at 04:52
  • Because when I change my email backend to console.It sends the email by printing that to the console. That means send_mail is working fine. – Ubaid Parvaiiz Apr 18 '20 at 04:53
  • Google is weird... This might send you down the right rabbit hole... https://www.digitalocean.com/community/questions/django-gmail-smtpauthenticationerror Everything your doing looks good. – James Powis Apr 18 '20 at 05:05
  • Also see if this works. I just confirmed it on my end. from smtplib import SMTP; s = SMTP('smtp.gmail.com', 587); s.starttls(); s.login('user', 'pass') – James Powis Apr 18 '20 at 05:11
  • When I run this piece of code "SMTP('smtp.gmail.com', 587)" I get the same traceback that I have listed in my question. – Ubaid Parvaiiz Apr 18 '20 at 05:19
  • Also, I tried the solutions that were in the link you provided me, but that still didn't resolve the error. – Ubaid Parvaiiz Apr 18 '20 at 05:23
  • 1
    your problem is absolutely DNS resolution, you mentioned CLI works but python does not... but this is a systems issue absolutely. – James Powis Apr 18 '20 at 05:26
  • So how can I solve that.I remember few months before I was tinkering with some inner python modules,propbably I think I have done something bad there. – Ubaid Parvaiiz Apr 18 '20 at 05:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/211919/discussion-between-james-powis-and-ubaid-parvaiiz). – James Powis Apr 18 '20 at 05:36

0 Answers0