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

smtplib.SMPT fails (gmail)

Fri Sep 12, 2014 8:07 am

Hello,
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 = '***@gmail.com'
password = '***'
destination = '***@gmail.com'

msg = 'test connection mail'

server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(username, destination, msg)
server.quit()
When I add some test prints, I notice the script hangs at

Code: Select all

server = smtplib.SMTP('smtp.gmail.com:587')
Already tried changing this to

Code: Select all

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

Code: Select all

  File "testsendmail.py", line 9, in <module>
    server = smtplib.SMTP('smtp.gmail.com', 587)
  File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.7/smtplib.py", line 310, in connect
    (code, msg) = self.getreply()
  File "/usr/lib/python2.7/smtplib.py", line 354, in getreply
    line = self.file.readline()
  File "/usr/lib/python2.7/socket.py", 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?

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

Re: smtplib.SMPT fails (gmail)

Fri Sep 12, 2014 11:01 pm

Change

server = smtplib.SMTP('smtp.gmail.com:587')

to

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

User avatar
DougieLawson
Posts: 35542
Joined: Sun Jun 16, 2013 11:19 pm
Location: 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 = datetime.now().strftime('%a %d %b @ %H:%M')

toaddr = '[email protected]'    # redacted
me = '[email protected]' # 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')
msg.attach(plainText)
msg.attach(htmlText)

try:
   s = smtplib.SMTP('smtp.gmail.com',587)
   s.starttls()
   s.login(me,password)
   s.send_message(msg)
   s.quit()
except:
   print ("Error: unable to send email")
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
jojopi
Posts: 3078
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

[email protected] ~ $ telnet smtp.gmail.com 587
Trying 64.233.167.109...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP gj3sm3395027wib.15 - gsmtp
If not, it is an external or network problem, rather than Python.

Concie
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

guyc
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;

server=smtplib.SMTP('smtp.gmail.com',587)

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 ([email protected] ~ $ telnet smtp.gmail.com 587) and I get the following response;

Trying 74.125.133.109...
Connected to gmail-smtp-msa.l.google.com.
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('smtp.gmail.com',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”