4.24.2011

mogmail with Python

Python版mogmail

・mogmail.py

   1: # Copyright (c) 2011 Mog Project. All rights reserved.
   2: import poplib
   3: import email
   4: from email.header import make_header
   5: from email.header import decode_header
   6: from email.utils import parsedate_tz
   7: from email.utils import mktime_tz
   8: import time
   9:  
  10: POP3_SERVER = 'POP3-Server'
  11: POP3_PORT = 110
  12: POP3_USERNAME = 'UserName'
  13: POP3_PASSWORD = 'Password'
  14: LIST_COUNT = 5
  15:  
  16: def main():
  17:   pop3 = poplib.POP3(POP3_SERVER, POP3_PORT)
  18:   pop3.apop(POP3_USERNAME, POP3_PASSWORD)
  19:  
  20:   for p in pop3.list()[1][:-1 - LIST_COUNT:-1]:
  21:     msg_no = p.split(' ')[0]
  22:     msg = email.message_from_string('\n'.join(pop3.retr(msg_no)[1]))
  23:  
  24:     f = lambda str: unicode(make_header(decode_header(msg[str])))
  25:  
  26:     try:
  27:       print time.strftime('[%m/%d %H:%M]',
  28:           time.localtime(mktime_tz(parsedate_tz(f('Date'))))),
  29:       print '%s\n  %s' % (
  30:           f('From').replace('"', ''), f('Subject'))
  31:     except:
  32:       print '*** Error ***'
  33:  
  34: if __name__ == '__main__':
  35:   main()

POP3サーバ名および認証情報はハードコーディング。

 

[2011/10/12]
改良版はこちら:
http://mogproject.blogspot.com/2011/10/mogmail-with-python-version-11.html

0 件のコメント:

コメントを投稿