2017-01-07 16 views
0

'をmessage_from_string' 誰もが、これは私の出力PythonのIMAP Gmailのパーサーエラー: 'モジュール' オブジェクトが属性を持っていない

In email account, accessing "%s" All mail... 

処理10通の電子メールでこのパーサ

import getpass, imaplib, email 

def split_addrs(s): 

    if not(s): return [] 
    outQ = True 
    cut = -1 
    res = [] 
    for i in range(len(s)): 
     if s[i]=='"': outQ = not(outQ) 
     if outQ and s[i]==',': 
      res.append(email.utils.parseaddr(s[cut+1:i])) 
      cut=i 
    res.append(email.utils.parseaddr(s[cut+1:i+1])) 
    return res 

def get_addresses(name, password): 

    mail=imaplib.IMAP4_SSL('imap.gmail.com') 
    mail.login(name , password) 
    print "In email account, accessing \"%s\" All mail...\n" 
    mail.select('"[Gmail]/All Mail"') 
    result,data=mail.search(None,"ALL") 
    ids=data[0].split()[-10:] 
    print "Processing %d emails...\n" % (len(ids)) 
    msgs = mail.fetch(','.join(ids),'(BODY.PEEK[HEADER])')[1][0::2] 
    addr=[] 
    print msgs 
    for x,msg in msgs: 
     msgobj = email.message_from_string(msg) 
     addr.extend(split_addrs(msgobj['to'])) 
     addr.extend(split_addrs(msgobj['from'])) 
     addr.extend(split_addrs(msgobj['cc'])) 

    mail.close() 
    mail.logout() 
    addr_set = set(addr) 
    print "Fetched and writing %d unique name and emails to scraped_email.txt...\n" % (len(addr_set)) 
    f = open('scraped_emails.txt', 'w') 
    for each in addr_set: 
     name, address = each 
     print "%s, %s" % (name, address) 
     f.write("%s, %s\n" % (name, address)) 
    f.close() 
    print "Done" 


if __name__ == '__main__': 

    email_address = raw_input('Enter your email: ') 
    print "Username: %s" % (email_address) 
    get_addresses(email_address, getpass.getpass()) 

を支援することができます...

Traceback (most recent call last): 
    File "/home/richard/environments/google-tools/gmail-addresses/program.py", line 52, in <module> 
    get_addresses(email_address, getpass.getpass()) 
    File "/home/richard/environments/google-tools/gmail-addresses/program.py", line 30, in get_addresses 
    msgobj = email.message_from_string(msg) 
AttributeError: 'module' object has no attribute 'message_from_string' 

フォルダを後で選択できるようにする予定ですが、今はすべてのメールで機能させるようにしたいと考えていました。

は、誰もが私のエラー

私は日食PyDevはIDEのとのpython 2.7を使用してい

おかげ

答えて

0

すべてがそうのpython 3ヘルプ

が不要でうまく働いた見つけることができます
関連する問題