Tom Jerram
Tom Jerram

Reputation: 11

Error when sending email with python smtplib

I am having trouble using smtplib to send emails

here is the code

import smtplib
port = 587

sender_email='************'
password='***********'

reciever_email='*************'

s = smtplib.SMTP('smtp.titan.mail',465)

s.starttls()

s.login(sender_email,password)

message ="""\
subject:  python email

this message is sent from Python"""

s.sendmail(sender_email,reciever_email,message)

s.quit()

here are the errors:

Traceback (most recent call last):
  File "tejmail.py", line 8, in <module>
    s = smtplib.SMTP('smtp.titan.mail',465)
  File "/usr/lib/python3.8/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python3.8/smtplib.py", line 339, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python3.8/smtplib.py", line 310, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "/usr/lib/python3.8/socket.py", line 787, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

Help please.

Upvotes: 1

Views: 664

Answers (1)

nimic
nimic

Reputation: 65

I am having this same problem. In my case it is because I'm behind some rather aggressive firewalls on a school network. It's hosting its own DNS name resolution but I don't have access to the local DNS server.

My work around was using replit.com and sending the data that needs to be mailed to the replit API and then the replit VM can send the email. Obviously not a great solution. I also have to run the replit program and my main one all the time for it to work.

Another possible solution is running your own DNS server with just the cache of the websites you need to communicate with.

Upvotes: 1

Related Questions