Posts: 2
Joined: Fri Sep 12, 2014 8:02 am

smtplib.SMPT fails (gmail)

Fri Sep 12, 2014 8:07 am

I try to run a simple script to send me an e-mail, but it fails
Here is the code:

Code: Select all

import smtplib

username = '***'
password = '***'
destination = '***'

msg = 'test connection mail'

server = smtplib.SMTP('')
server.sendmail(username, destination, msg)
When I add some test prints, I notice the script hangs at

Code: Select all

server = smtplib.SMTP('')
Already tried changing this to

Code: Select all

server = smtplib.SMTP('', 587)
When I press ctrl+C, this is what I get:

Code: Select all

  File "", line 9, in <module>
    server = smtplib.SMTP('', 587)
  File "/usr/lib/python2.7/", line 249, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.7/", line 310, in connect
    (code, msg) = self.getreply()
  File "/usr/lib/python2.7/", line 354, in getreply
    line = self.file.readline()
  File "/usr/lib/python2.7/", line 447, in readline
    data = self._sock.recv(self._rbufsize)
When I run this code on my windows machine, there is no problem

Is there someone who can help me with this problem?

Posts: 44
Joined: Fri Apr 05, 2013 10:42 pm

Re: smtplib.SMPT fails (gmail)

Fri Sep 12, 2014 11:01 pm


server = smtplib.SMTP('')


server = smtplib.SMTP('', 587)
You could say my love for Linux is terminal ;)

User avatar
Posts: 39157
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: smtplib.SMPT fails (gmail)

Sat Sep 13, 2014 12:53 am

This code works with python3

Code: Select all

import time
from datetime import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

f_time ='%a %d %b @ %H:%M')

toaddr = ''    # redacted
me = '' # redacted
password = 'xxxxxxxx' # redacted
subject = 'Test message ' + f_time

msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = me
msg['To'] = toaddr
msg.preamble = "Test message @ " + f_time
message = "Main body text here"
html = "<html><head></head><body><h1>Title</h1><p>Main body text here</body></html>"
plainText = MIMEText(message, 'plain')
htmlText = MIMEText(html,'html')

   s = smtplib.SMTP('',587)
   print ("Error: unable to send email")
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
Posts: 3272
Joined: Tue Oct 11, 2011 8:38 pm

Re: smtplib.SMPT fails (gmail)

Sat Sep 13, 2014 1:28 am

The code is fine. From the backtrace it has connected to gmail but is still waiting to read the connection greeting message. That should not take long.

Do you get the greeting if you telnet to gmail manually?

Code: Select all

pi@tau ~ $ telnet 587
Connected to
Escape character is '^]'.
220 ESMTP gj3sm3395027wib.15 - gsmtp
If not, it is an external or network problem, rather than Python.

Posts: 2
Joined: Fri Sep 12, 2014 8:02 am

Re: smtplib.SMPT fails (gmail)

Sun Sep 14, 2014 9:59 am

I've decided to completely reinstall my Pi with the latest version of Raspbian.
This seemd to fix the problem for me

Thank you all for your replies

Posts: 4
Joined: Wed Oct 22, 2014 8:27 pm

Re: smtplib.SMPT fails (gmail)

Wed Mar 18, 2015 9:32 pm

I am having the exact same problem. My code hangs at;


It will not return unless a do a manual ctrl-c.

I am running Python 3.2.3 on Raspbian GNU/Linux 3.12.28+ OS

I tried to telnet (pi@raspberrypi ~ $ telnet 587) and I get the following response;

Connected to
Escape character is '^]'.

It appears the connection has been made but I get no other output.
I have a couple questions;

1) Why does smtplib.SMTP('',587) hang and not timeout regardless if the problem is externally related?
2) How can I avoid a hang condition like this?
3) Has anybody run into this issue and understand the root cause of the hang?
4) Any other suggestions?

Return to “Python”