我写的一个终端邮件客服端脚本,利用imap协议

由本社区发起的开源项目
回复
头像
tonychen123
帖子: 101
注册时间: 2009-04-03 20:52
来自: Guangzhou -China
送出感谢: 0
接收感谢: 0

我写的一个终端邮件客服端脚本,利用imap协议

#1

帖子 tonychen123 » 2009-11-25 14:27

代码如下:

代码: 全选

# Simple script that uses the python imap library
# to retrieve new unread messages

import imaplib, email

usn = 'user_name' # user name
psd = 'your_password'   # password
mbx = 'INBOX'       # inbox is default

msr = 'mail.genomics.org.cn'    # mail server
# prt = '993'         # port

svr = imaplib.IMAP4(msr)   # server, or svr = imaplib.IMAP4_SSL(msr, prt) 
svr.login(usn, psd)
svr.select(mbx, 'readonly')

raw_msgs = []

tpe, new_msgs = svr.search(None, 'unseen')
for num in new_msgs[0].split():
    tpe, raw_msg = svr.fetch(num, '(RFC822)')
    msg = email.message_from_string(raw_msg[0][1])
    
    # Subjects
    sbj, ecode = email.Header.decode_header(msg['subject'])[0]
    #if ecode:
    #    sbj = sbj.decode('gbk')
        
    # From, sender
    frm = ''    
    for fts, ecode in email.Header.decode_header(msg['from']):
        #if ecode:
        #    fts =  fts.decode('gbk')
        frm = frm + fts 
    
    # To, recervers
    rcv = ''    
    for rv, ecode in email.Header.decode_header(msg['to']):
        #if ecode:
        #    rv =  fts.decode(ecode)
        rcv = rcv + rv
    
    # Date
    dte = msg['date']
    
    # Content
    cnt = msg.get_payload(decode=msg['Content-Transfer-Encoding'])
    
    print \
'''
=================================================================
                  %s
=================================================================
''' % (sbj)
    
    print "From:\t%s\n\nTo:\t%s\n\nDate:\t%s\n\nSubject:\n\t%s\n\nContent:\n\t%s" % (frm, rcv, dte, sbj, cnt)

svr.close()
svr.logout()
效果如下:
screenshot.png
There should be one-- and preferably only one --obvious way to do it.
头像
tonychen123
帖子: 101
注册时间: 2009-04-03 20:52
来自: Guangzhou -China
送出感谢: 0
接收感谢: 0

Re: 我写的一个终端邮件客服端脚本,利用imap协议

#2

帖子 tonychen123 » 2009-11-25 14:42

该脚本+genome-osd+crontab,就可以定制你的邮件提醒服务了
There should be one-- and preferably only one --obvious way to do it.
回复

回到 “开源小工具”