Skip to content Skip to sidebar Skip to footer

Sending An Email With Python Issue

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

Solution 1:

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 = "yourgmailaccount@gmail.com"        
toname = "Recipient"
toemail = "recipient@other.com"
subject = "Test Mail"
body = "Body....."

notify(fromname, fromemail, toname, toemail, subject, body, password)

Post a Comment for "Sending An Email With Python Issue"