TIMEX
TIMEX

Reputation: 271594

Why does my Python email script have an error?

from smtplib import SMTP_SSL as SMTP
from email.MIMEText import MIMEText
import traceback

#from_field is a dictionary with smtp_host, name, email, and password
def send(from_field, to_email):    
    subject = 'how do you know it'
    body="""\
    hello there
    """

    try:
        msg = MIMEText(body, 'plain')
        msg['Subject']= subject
        msg['From']   =  from_field['name'] + ' <'+from_field['email']+'>'

        print msg

        conn = SMTP(from_field['smtp'])
        conn.set_debuglevel(False)
        conn.login(from_field['email'],from_field['password'])
        try:
            conn.sendmail(from_field.email,[to_email], msg.as_string())
            pass
        finally:
            conn.close()

    except Exception, exc:
        traceback.print_exc()
        sys.exit( "mail failed; %s" % str(exc) ) # give a error message    

I get this error message when I run it:

AttributeError: 'dict' object has no attribute 'email' on the conn.sendmail line

Upvotes: 0

Views: 94

Answers (1)

Bill the Lizard
Bill the Lizard

Reputation: 405675

from_field is a dictionary. That line should be:

conn.sendmail(from_field['email'], to_email, msg.as_string())

Upvotes: 2

Related Questions