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

Emile van Sebille emile at fenx.com
Tue Sep 11 23:26:50 CEST 2012


On 9/11/2012 2:19 PM ashish makani said...
> 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 <http://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 <mailto:x at gmail.com>', 'y at gmail.com
> <mailto: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) :(

Good -- the university is taking steps to block spam.

You should send mail using the university mail system and not 
smtp.gmail.com.

Emile




More information about the Tutor mailing list