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
运行时收到此错误消息:
conn.sendmail行 AttributeError: 'dict' object has no attribute 'email'
答案 0 :(得分:2)
from_field
是一本字典。该行应该是:
conn.sendmail(from_field['email'], to_email, msg.as_string())