[Tutor] How to send email from a gmail a/c using smtp when port 587(smtp) is blocked

ashish makani ashish.makani at gmail.com
Tue Sep 11 23:19:23 CEST 2012

Hi Python Tutor folks

I am stuck with an issue, so am coming to the Pythonistas who rescue me
everytime :)

I am trying to send out email programmatically, from a gmail a/c, using
smtplib, using the following chunk of code (b/w [ & ] below)


import smtplib
from email.mime.text import MIMEText

#uname, pwd are username & password of gmail a/c i am trying to send from

server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls() # get response(220, '2.0.0 Ready to start TLS')
server.login(uname,pwd)  # get response(235, '2.7.0 Accepted')

toaddrs  = ['x at gmail.com', 'y at gmail.com' ] # list of To email addresses
msg = MIMEText('email body')
msg['Subject'] = 'email subject'
server.sendmail(fromaddr, toaddrs, msg.as_string())


The code above works perfectly fine on my local machine, but fails on the
production server at the university where i work( all ports other than port
80 are blocked) :(

So , when i try to run the 2 py statements (in bold below) on a python
prompt from the production server, which has port 587 blocked, i get the
following error

*>>> import smtplib*
*>>> server = smtplib.SMTP('smtp.gmail.com:587')*
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/smtplib.py", line 239, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.6/smtplib.py", line 295, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python2.6/smtplib.py", line 273, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "/usr/lib/python2.6/socket.py", line 514, in create_connection
    raise error, msg
socket.error: [Errno 101] Network is unreachable


1. How can i overcome this ?
A friend suggested , that i could use something called smtp relay, which
would solve my problem, but would be time-consuming & a pain to set up.
I did some cursory googling & searching on stackoverflow but could not find
any good, well explained results.
I dont know anything about SMTP.
Anybody has any recommendations on a good explanation on smtp relay & how
to set it up for sending email from a gmail a/c using a python script ?

2. Also, is there a more elegant/cleaner/graceful solution to my problem
than using an smtp relay ?

Any & all explanations/links/code
snippets/thoughts/ideas/suggestions/feedback/comments/ of the Python tutor
community would be greatly appreciated.

Thanks a ton


email :
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120912/184b4a3a/attachment.html>

More information about the Tutor mailing list