Failing to send email with the Python example

Chatter picture Chatter · Dec 30, 2008 · Viewed 65.8k times · Source

I've been trying (and failing) to figure out how to send email via Python.

Trying the example from here: http://docs.python.org/library/smtplib.html#smtplib.SMTP

but added the line server = smtplib.SMTP_SSL('smtp.gmail.com', 465) after I got a bounceback about not having an SSL connection.

Now I'm getting this:

Traceback (most recent call last):
  File "C:/Python26/08_emailconnects/12_29_EmailSendExample_NotWorkingYet.py", line 37, in <module>
    server = smtplib.SMTP('smtp.gmail.com', 65)
  File "C:\Python26\lib\smtplib.py", line 239, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python26\lib\smtplib.py", line 295, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Python26\lib\smtplib.py", line 273, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "C:\Python26\lib\socket.py", line 512, in create_connection
    raise error, msg
error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
>>> 

Thoughts?


server = smtplib.SMTP("smtp.google.com", 495) gives me a timeout error. just smtplib.smtp("smtp.google.com", 495) gives me "SSLError: [Errno 1] _ssl.c:480: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol" (see below).

I'm trying different ports and now I'm getting a completely new error. I'll just post the whole bit of code, I'm probably making some rookie mistake.

"

import smtplib

mailuser = '[email protected]'

mailpasswd = 'MYPASSWORD'

fromaddr = '[email protected]'

toaddrs = '[email protected]'

msg = 'Hooooorah!'

print msg

server = smtplib.SMTP_SSL('smtp.google.com')

server = smtplib.SMTP_SSL_PORT=587

server.user(mailuser)

server.pass_(mailpasswd)

server.set_debuglevel(1)

server.sendmail(fromaddr, toaddrs, msg)

server.quit()

"

and then I get this error message: "

Traceback (most recent call last):
  File "C:/Python26/08_emailconnects/12_29_eMAILSendtryin_stripped.py", line 16, in <module>
    server = smtplib.SMTP_SSL('smtp.google.com')
  File "C:\Python26\lib\smtplib.py", line 749, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout)
  File "C:\Python26\lib\smtplib.py", line 239, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python26\lib\smtplib.py", line 295, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Python26\lib\smtplib.py", line 755, in _get_socket
    self.sock = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
  File "C:\Python26\lib\ssl.py", line 350, in wrap_socket
    suppress_ragged_eofs=suppress_ragged_eofs)
  File "C:\Python26\lib\ssl.py", line 118, in __init__
    self.do_handshake()
  File "C:\Python26\lib\ssl.py", line 293, in do_handshake
    self._sslobj.do_handshake()
SSLError: [Errno 1] _ssl.c:480: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

"

note that actually the which looks like "server = smtplib.SMTPSSLPORT=587" is actually "server = smtplib.SMTP underscore SSL underscore PORT=587", there's some sort of formatting thing going on here.

Answer

Federico A. Ramponi picture Federico A. Ramponi · Dec 30, 2008

The following code works for me:

import smtplib

FROMADDR = "[email protected]"
LOGIN    = FROMADDR
PASSWORD = "my.real.password"
TOADDRS  = ["[email protected]"]
SUBJECT  = "Test"

msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
       % (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
msg += "some text\r\n"

server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()

I'm using Python 2.5.2.

Edit: changed port from 25 to 587 as suggested by ΤΖΩΤΖΙΟΥ, and dropped the second ehlo(). Now I would love to know why port 25 works perfectly from my machine (and port 465 does not).