最近因为公司是做邮件行业的,所以用Python 写了一个登录邮箱 imap的程序,主要功能是 登录到相应的邮件地址,查找邮件在收件箱还是垃圾箱等,并提取邮件头的相应信息如:邮件主题、发送域、发送IP等,程序如下:
#!/usr/bin/python
import imaplib, email, re, string
from datetime import datetime, timedelta
from email.parser import Parser
from email.Utils import parseaddr
from email.Header import decode_header
def getmailheader(header_text, default="ascii"):
"""Decode header_text if needed"""
try:
headers=decode_header(header_text)
except email.Errors.HeaderParseError:
# This already append in email.base64mime.decode()
# instead return a sanitized ascii string
return header_text.encode('ascii', 'replace').decode('ascii')
else:
for i, (text, charset) in enumerate(headers):
try:
headers[i]=unicode(text, charset or default, errors='replace')
except LookupError:
# if the charset is unknown, force default
headers[i]=unicode(text, default, errors='replace')
return u"".join(headers)
obj = imaplib.IMAP4('mail.sohu.com')
obj.login('ransheroo@sohu.com','aklksbbl')
obj.select('INBOX')
typ, data = obj.uid('search', None, 'ALL')
num = string.split(data[0])[-1]
print num
typ, data = obj.uid('fetch', num, '(RFC822)')
msg = email.message_from_string(data[0][1])
received = msg.get_all('Received', '')
ip = re.search(r'\[(.*)\]', received[1]).group(1)
subject = getmailheader(msg.get('Subject', ''))
the_from = msg.get('From', '')
from_address = re.search(r'<(.*)>',the_from).group(1)
sender = msg.get('X-Sender', '')
if "<" in sender:
sender = re.search(r'<(.*)>',sender).group(1)
print "Mail's subject is %s" % subject
print "Mail's ip is %s" % ip
print "Mail's from_address is %s" % from_address
print "Mail's sender is %s" % sender