1

I have this code and I cannot seem to get it to work. When I run it, the script doesn't finish in IDLE unless I kill it manually. I have looked all over and rewritten the code a few times, and no luck.

import smtplib SMTP_SERVER = 'smtp.gmail.com' SMTP_PORT = 587 sender = '[email protected]' password = '123' recipient = '[email protected]' subject = 'Test Results' body = """** AUTOMATED EMAIL ** \r\n Following are the test results: \r\n""" headers = ["From: " + sender, "Subject: " + subject, "To: " + recipient] headers = "\r\n".join(headers) try: session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) session.ehlo() session.starttls() session.ehlo() session.login(sender, password) session.sendmail(sender, recipient, headers + "\r\n\r\n" + body) except smtplib.SMTPException: print "Error: Unable to send email." session.quit() 
4
  • Are there any error logs you could check to see what is happening at the point where the script gets stuck? Commented Jan 8, 2014 at 22:41
  • Try changing your except line to except smtplib.SMTPException as e: then print e Commented Jan 8, 2014 at 22:42
  • @wnnmaw I tried that, but the same thing happens, the cursor just blinks and the script doesn't finish, I have to kill it manually. Commented Jan 10, 2014 at 22:04
  • @summea I don't know how to check the error logs...? Commented Jan 10, 2014 at 22:23

1 Answer 1

2

Not sure why you're using ehlo; contrary to popular opinion, it's not actually required so long as you set the headers correctly. Here's a tested and working script -- it works on *nix and OSX. Since you're using Windows though, we need to troubleshoot further.

import smtplib, sys def notify(fromname, fromemail, toname, toemail, subject, body, password): fromaddr = fromname+" <"+fromemail+">" toaddrs = [toname+" <"+toemail+">"] msg = "From: "+fromaddr+"\nTo: "+toemail+"\nMIME-Version: 1.0\nContent-type: text/plain\nSubject: "+subject+"\n"+body # Credentials (if needed) username = fromemail password = password # The actual mail send try: server = smtplib.SMTP('smtp.gmail.com:587') server.starttls() server.login(username,password) server.sendmail(fromaddr, toaddrs, msg) server.quit() print "success" except smtplib.SMTPException: print "failure" fromname = "Your Name" fromemail = "[email protected]" toname = "Recipient" toemail = "[email protected]" subject = "Test Mail" body = "Body....." notify(fromname, fromemail, toname, toemail, subject, body, password) 
Sign up to request clarification or add additional context in comments.

26 Comments

I copied this script and changed the fromname etc. and added in password, but the same thing is still happening to me. It runs but the cursor just flashes and it doesn't finish unless I kill it.
That's definitely a send mail problem. See if you can successfully send mail from the command line using sendmail? Next step is toe check the logs.
I'm sorry but I don't know what you are talking about. I opened cmd.exe and typed sendmail and it said it is not recognized...
Oh, Windows is a different story. You won't have sendmail to test with. Also I wrote this wrong: change the except SMTPException: to except smtplib.SMTPException:. Try purposefully mistyping the smtp url to see if it's correctly handling errors (e.g. smtpa.gmail.com:587).
Wondering if it's the fact that it's Windows that's causing the problem? Somehow you need to find the Windows smtplib logs. Can you use PrtQryUI to make sure you can access the google server on 587?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.