sending gmail with smtplib from behind a proxy

Bjorn bjornjobb at gmail.com
Thu Feb 26 06:07:21 EST 2009


Hi, I tried the script below to send mail by gmail from python using
smptlib. I got the script here (http://kutuma.blogspot.com/2007/08/
sending-emails-via-gmail-with-python.html)

It works fine from home, but at work not. I think it is because I am
behind a http or a socks proxy server at work. Can I adapt this script
to work from behind the http or socks proxy? I couldnt understand from
the smtplib if I can specify this somehow?

Sincerely,
bjorn johansson

----Script---------
#!/usr/bin/python

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os

gmail_user = "your_email at gmail.com"
gmail_pwd = "your_password"

def mail(to, subject, text, attach):
   msg = MIMEMultipart()

   msg['From'] = gmail_user
   msg['To'] = to
   msg['Subject'] = subject

   msg.attach(MIMEText(text))

   part = MIMEBase('application', 'octet-stream')
   part.set_payload(open(attach, 'rb').read())
   Encoders.encode_base64(part)
   part.add_header('Content-Disposition',
           'attachment; filename="%s"' % os.path.basename(attach))
   msg.attach(part)

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(gmail_user, gmail_pwd)
   mailServer.sendmail(gmail_user, to, msg.as_string())
   # Should be mailServer.quit(), but that crashes...
   mailServer.close()

mail("some.person at some.address.com",
   "Hello from python!",
   "This is a email sent with python",
   "my_picture.jpg")



More information about the Python-list mailing list