Properly formatted example for Python iMAP email access?

前端 未结 4 1939
故里飘歌
故里飘歌 2021-02-01 09:57

tldr: Can someone show me how to properly format this Python iMAP example so it works?

from https://docs.python.org/2.4/lib/imap4-example.html

<         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 10:32

    Here is a script I used to use to grab logwatch info from my mailbox. Presented at LFNW 2008 -

    #!/usr/bin/env python
    
    ''' Utility to scan my mailbox for new mesages from Logwatch on systems and then
        grab useful info from the message and output a summary page.
    
        by Brian C. Lane 
    '''
    import os, sys, imaplib, rfc822, re, StringIO
    
    server  ='mail.brianlane.com'
    username='yourusername'
    password='yourpassword'
    
    M = imaplib.IMAP4_SSL(server)
    M.login(username, password)
    M.select()
    typ, data = M.search(None, '(UNSEEN SUBJECT "Logwatch")')
    for num in data[0].split():
        typ, data = M.fetch(num, '(RFC822)')
    #   print 'Message %s\n%s\n' % (num, data[0][1])
    
        match = re.search(  "^(Users logging in.*?)^\w",
                            data[0][1],
                            re.MULTILINE|re.DOTALL )
        if match:
            file = StringIO.StringIO(data[0][1])
            message = rfc822.Message(file)
            print message['from']
            print match.group(1).strip()
            print '----'
    
    M.close()
    M.logout()
    

提交回复
热议问题