#!/usr/bin/env python2.7
#-*- encoding: utf-8 -*-
import imaplib
import email
#设置命令窗口输出使用中文编码
import sys
reload(sys)
sys.setdefaultencoding('gbk')
ssl=False
port = 143
#保存文件方法(都是保存在指定的根目录下)
def savefile(filename, data, path):
try:
filepath = path + filename
print 'Saved as ' + filepath
f = open(filepath, 'wb')
except:
print('filename error')
f.close()
f.write(data)
f.close()
#字符编码转换方法
def my_unicode(s, encoding):
if encoding:
return unicode(s, encoding)
else:
return unicode(s)
#获得字符编码方法
def get_charset(message, default="ascii"):
#Get the message charset
return message.get_charset()
return default
#解析邮件方法(区分出正文与附件)
def parseEmail(msg, mypath):
mailContent = None
contenttype = None
suffix =None
for part in msg.walk():
if not part.is_multipart():
contenttype = part.get_content_type()
filename = part.get_filename()
charset = get_charset(part)
#是否有附件
if filename:
h = email.Header.Header(filename)
dh = email.Header.decode_header(h)
fname = dh[0][0]
encodeStr = dh[0][1]
if encodeStr != None:
if charset == None:
fname = fname.decode(encodeStr, 'gbk')
else:
fname = fname.decode(encodeStr, charset)
data = part.get_payload(decode=True)
print('Attachment : ' + fname)
#保存附件
if fname != None or fname != '':
savefile(fname, data, mypath)
else:
if contenttype in ['text/plain']:
suffix = '.txt'
if contenttype in ['text/html']:
suffix = '.htm'
if charset == None:
mailContent = part.get_payload(decode=True)
else:
mailContent = part.get_payload(decode=True).decode(charset)
return (mailContent, suffix)
#获取邮件方法
def getMail(mailhost, account, password, diskroot, port = 143, ssl = 0):
#mypath = diskroot + ':\\'
mypath = diskroot
#是否采用ssl
if ssl == 1:
imapServer = imaplib.IMAP4_SSL(mailhost, 993)
else:
imapServer = imaplib.IMAP4(mailhost, 143)
imapServer.login(account, password)
imapServer.select()
#邮件状态设置,新邮件为Unseen
#Message statues = 'All,Unseen,Seen,Recent,Answered, Flagged'
resp, items = imapServer.search(None, "All")
number = 1
for i in items[0].split():
#get information of email
resp, mailData = imapServer.fetch(i, "(RFC822)")
mailText = mailData[0][1]
msg = email.message_from_string(mailText)
ls = msg["From"].split(' ')
strfrom = ''
if(len(ls) == 2):
fromname = email.Header.decode_header((ls[0]).strip('\"'))
strfrom = 'From : ' + my_unicode(fromname[0][0], fromname[0][1]) + ls[1]
else:
strfrom = 'From : ' + msg["From"]
strdate = 'Date : ' + msg["Date"]
subject = email.Header.decode_header(msg["Subject"])
sub = my_unicode(subject[0][0], subject[0][1])
strsub = 'Subject : ' + sub
mailContent, suffix = parseEmail(msg, mypath)
#命令窗体输出邮件基本信息
print '\n'
print 'No : ' + str(number)
print strfrom
print strdate
print strsub
'''
print 'Content:'
print mailContent
'''
#保存邮件正文
if (suffix != None and suffix != '') and (mailContent != None and mailContent != ''):
savefile(str(number) + suffix, mailContent, mypath)
number = number + 1
imapServer.close()
imapServer.logout()
#if __name__ =="__main__":
# mypath ='/home/Jack/Mailtest'
# print 'begin to get email...'
# getMail('172.16.0.100', '[email protected]', 'q1w2E#R$', 'mypath', 143, 0)
#126邮箱登陆没用ssl
#getMail('imap.126.com', '[email protected]', 'xxxxxxxxxx', mypath, 143, 0)
# print 'the end of get email.'
def print_usage():
print("Usage: ")
print( " %s mail_host email_addr password diskroot port ssl(1)" % (sys.argv[0]))
if __name__ == '__main__':
# host = "172.16.0.100"
# username = "[email protected]"
# password = "q1w2E#R$"
reload(sys)
sys.setdefaultencoding('utf8')
# if len(sys.argv) == 7:
# mailhost = sys.argv[1]
# account = sys.argv[2]
# password = sys.argv[3]
# diskroot = sys.argv[4]
# port == sys.argv[5]
# ssl == sys.argv[6]
with open('ip.txt') as f:
for line in f:
line = line.strip()
mailhost,account,password,diskroot,port,ssl = line.split(':')
print 'begin to get mail.........'
getMail(mailhost,account,password,diskroot,port,ssl)
print 'end of get mail'
#126邮箱登陆没用ssl
#getMail('imap.126.com', '[email protected]', 'xxxxxxxxxx', mypath, 143, 0)
# print 'the end of get email.'