2015-04-22 166 views
-1

我正在尝试获取电子邮件,然后我们需要根据主题将邮件写入不同的文件。输入错误:'模块'对象没有任何属性'_getitem_'

import email 
import imaplib 
from threading import Thread 
from time import sleep 
import time 

def myThreadFun(): 

    M = imaplib.IMAP4_SSL('imap.gmail.com') 
    M.login("[email protected]", "embeddedSystems") 



    while (1): 

     M.select() 

     rv, data1 = M.search(None, 'UNSEEN') 

     for i in data1[0].split(): 

      resp, data = M.FETCH(i, '(RFC822)') 
      mail = email.message_from_string(data[0][1]) 

      for part in mail.walk(): 

       # multipart are just containers, so we skip them 
       if part.get_content_maintype() == 'multipart': 
        continue 

       # we are interested only in the simple text messages 
       if part.get_content_subtype() != 'plain': 
        continue 



       payload = part.get_payload() 
       print '\n' 
       print '[%s]' % (mail['Subject']) 
       print 'From: %s' % (mail['From']) 
       print 'Date:', mail['Date'] 
       print '=================================' 
       print payload 
       #time.sleep(10) 

       #save_string = str("/home/buddhi-xubuntu/Python/email_" + ".text") 
       #myfile = open(save_string, 'a') 
       #myfile.write(mail['Subject']+ "\nFrom: " + mail['From'] + "\nDate: " + mail['Date'] + "=============\n" + payload) 
       #myfile.close() 
       #time.sleep(10) 


       #with file('email_.txt', 'r') as original: data = original.read() 
       #with file('email_2.txt', 'w') as modified: modified.write(mail['Subject']+ "\nFrom: " + mail['From'] + "\nDate: " + mail['Date'] + "\n=============\n" + payload + "\n" + data) 

       #orig_string = str("/home/e11452/Downloads/email_" + ".text") 
       #f = open(orig_string,'r') 
       #temp = f.read() 
       #f.close() 


       if mail['Subject']=="E/11": 
        new_string = str("/home/e11452/Downloads/email_11" + ".text") 
        f = open(new_string, 'w') 
        f.write(mail['Subject']+ "\nFrom: " + mail['From'] + "\nDate: " + mail['Date'] + "\n=============\n" + payload + "\n") 

       elif mail['Subject']=="E/10": 
        new_string = str("/home/e11452/Downloads/email_12" + ".text") 
        -f = open(new_string, 'w') 
        f.write(mail['Subject']+ "\nFrom: " + mail['From'] + "\nDate: " + mail['Date'] + "\n=============\n" + payload + "\n") 


       f.write(temp) 
       f.close() 


       time.sleep(10) 

    M.LOGOUT() 

thread = Thread(target = myThreadFun) 

thread.start() 

以上是我试过的代码,我得到一个错误说

回溯(最近通话最后一个):文件“email14.py” 58行,如果在电子邮件[“主题”] = = 'E/11':类型错误: '模块' 对象没有属性 '的GetItem'

+1

您的代码片段与该例外不匹配。 –

回答

0

似乎拼错mail作为emailemail是您导入的模块。然后你得到了错误。

+0

代码中的那一行在哪里? –

+0

正如错误提示,第58行。 – skyline75489

+0

你看到它吗? –

相关问题