How to send an email with GMail in Python from Windows

  • Thread starter ∂ √ ¡ Å‹ ∂ ♪ Ñ’
  • Start date
Â

∂ √ ¡ ŋ ∂ ♪ ђ

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 address removed)' # for SMTP AUTH, set SMTP username here
smtppass = '*****' # for SMTP AUTH, set SMTP password here

RECIPIENTS = ['(e-mail address removed)']
SENDER = '(e-mail address 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
 
G

gordyt

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 address removed)'], 'this is just a test',
"hello world!" )


--gordy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top