2013-06-23 30 views
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  

當我運行它,我得到這個錯誤信息:在conn.sendmail線爲什麼我的Python電子郵件腳本有錯誤?

回答

2

from_field

AttributeError: 'dict' object has no attribute 'email'是一本字典。該行應該是:

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

謝謝。應該「to_email」是一個列表嗎? – TIMEX

+0

@TIMEX從您發佈的代碼中很難說。它取決於'to_email'是什麼類型傳遞給'send'函數的。 –

相關問題