0
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    

运行时收到此错误消息:

AttributeError: 'dict' object has no attribute 'email'在 conn.sendmail 线上

4

1 回答 1

2

from_field是一本字典。那行应该是:

conn.sendmail(from_field['email'], to_email, msg.as_string())
于 2013-06-23T18:03:45.537 回答