0

I want to send emails from my Gmail account using python. I followed steps given in this stackoverflow post: How to send an email with Python?

But, my the mails that I sent do not reach the addresses.

This is the error that I get:

Traceback (most recent call last):
  File "something.py", line 24, in <module>
    server = smtplib.SMTP('myserver')
  File "/anaconda2/lib/python2.7/smtplib.py", line 256, in __init__
    (code, msg) = self.connect(host, port)
  File "/anaconda2/lib/python2.7/smtplib.py", line 317, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/anaconda2/lib/python2.7/smtplib.py", line 292, in _get_socket
    return socket.create_connection((host, port), timeout)
  File "/anaconda2/lib/python2.7/socket.py", line 557, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

What should I be doing here?

Anna
  • 31
  • 4
  • 3
    What is the error? Please be more specific – enamoria Nov 07 '18 at 02:05
  • 3
    Maybe destination has a spam protection. – BladeMight Nov 07 '18 at 02:05
  • Check if you can send an email from the command line. Google quit accepting smtp messages from users without a recognized domain. I had to use my msn account to do this. – bivouac0 Nov 07 '18 at 02:14
  • enamoria, BladeMight, bivouac0: I have updated the post with the error that I am getting. Let me know if this is helpful. – Anna Nov 07 '18 at 02:23
  • 1
    You're not meant to copy `server = smtplib.SMTP('myserver')` exactly, replace `'myserver'` with your email provider's SMTP server. See https://docs.python.org/3/library/smtplib.html for more on how to use smtplib. – Andrea Reina Nov 07 '18 at 02:56

2 Answers2

3

What you've get is a DNS query error indicating that domain myserver does not exist.

You have to replace the argument myserver in server = smtplib.SMTP('myserver') with the actual address of SMTP server, such as smtp.mail.yahoo.com.

Chang Qian
  • 1,091
  • 8
  • 15
0

This is how I do it.

import smtplib
server=smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login('your_email@gmail.com','your_password')
server.sendmail('your_email@gmail.com','your_email@gmail.com','test email')
ASH
  • 20,759
  • 19
  • 87
  • 200