Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to send an email with GMail in Python from Windows

Reply
Thread Tools

How to send an email with GMail in Python from Windows

 
 
∂ √ ¡ ŋ ∂ ♪ ђ
Guest
Posts: n/a
 
      03-12-2009
Hi
Can somebody help me with sending an email using Python from GMail
Here's what I tried but it fails always.

__________________________________________________ __________________
import smtplib
import base64

smtpserver = 'smtp.gmail.com'
AUTHREQUIRED = 0 # if you need to use SMTP AUTH set to 1
smtpuser = '(E-Mail Removed)' # for SMTP AUTH, set SMTP username here
smtppass = '*****' # for SMTP AUTH, set SMTP password here

RECIPIENTS = ['(E-Mail Removed)']
SENDER = '(E-Mail Removed)'
mssg = open('mssg.txt', 'r').read() # I am reading from this file on
the same directory

session = smtplib.SMTP(smtpserver,'465')
session.ehlo()
#session.esmtp_features["auth"] = "LOGIN PLAIN"
session.connect(smtpserver,'465')
session.ehlo()
session.starttls()
session.set_debuglevel(1)
session.helo()

if AUTHREQUIRED:
try:
session.login(smtpuser, smtppass)

except SMTPAuthenticationError, e:
# if login fails, try again using a manual plain login method
smtp.docmd("AUTH LOGIN", base64.b64encode( smtpuser ))
smtp.docmd(base64.b64encode( smtppass ), "")
smtpresult = session.sendmail(SENDER, RECIPIENTS, mssg)
if smtpresult:
errstr = ""
for recip in smtpresult.keys():
errstr = """Could not delivery mail to: %s

Server said: %s
%s

%s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
raise smtplib.SMTPException, errstr
__________________________________________________ __
This is the Stack Trace I got when I ran the above script after a very
long time(>15min)

Traceback (most recent call last):
File "C:\Python26\Mail.py", line 13, in <module>
session = smtplib.SMTP(smtpserver,'465')
File "C:\Python26\lib\smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python26\lib\smtplib.py", line 296, in connect
(code, msg) = self.getreply()
File "C:\Python26\lib\smtplib.py", line 340, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

Tool completed with exit code 1

__________________________________________________ ____________________
Thanks
Avinash
 
Reply With Quote
 
 
 
 
dorzey
Guest
Posts: n/a
 
      03-12-2009
You might want to try - http://libgmail.sourceforge.net/. This is a
Python binding for GMail; I've used it a little and it did the job for
me.

Dorzey
 
Reply With Quote
 
 
 
 
gordyt
Guest
Posts: n/a
 
      03-12-2009
Howdy Avinash,

Here is a simple example for you.

from smtplib import SMTP
HOST = "smtp.gmail.com"
PORT = 587
ACCOUNT = "" # put your gmail email account here
PASSWORD = "" # put your gmail email password here

def send_email(to_addrs, subject, msg):
server = SMTP(HOST,PORT)
server.set_debuglevel(1) # you don't need this (comment out to
avoid debug messages)
server.ehlo()
server.starttls()
server.ehlo()
server.login(ACCOUNT, PASSWORD)
server.sendmail(ACCOUNT, to_addrs,
"""From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s\r\n.\r\n""" % (
ACCOUNT, ",".join(to_addrs), subject, msg
)
)
server.quit()

if __name__ == "__main__":
send_email( ['(E-Mail Removed)'], 'this is just a test',
"hello world!" )


--gordy
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to send email programmatically from a gmail email a/c when port587(smtp) is blocked ashish makani Python 5 09-20-2012 07:56 AM
Re: smtplib send email by using gmail smtp server Gabriel Genellina Python 0 05-03-2009 06:08 PM
send email using gmail as smtp Junkone Ruby 3 12-20-2007 10:27 PM
6 gmail accounts available, post your email here or to gmail@thing.dyndns.org thing NZ Computing 6 12-01-2004 11:40 PM



Advertisments