-
Notifications
You must be signed in to change notification settings - Fork 0
/
cm.py
124 lines (103 loc) · 3.77 KB
/
cm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!user/bin python
#coding: utf-8
import imaplib,getpass,struct,datetime
import email,os,sys,re
from email.header import Header
reload(sys)
sys.setdefaultencoding('utf-8')
def Make_date_dir(work_path):
"""
make a new dir,20170815
if 20170815 is exist then,20170815_2
"""
date_dir = ""
date = datetime.date.today()
for i in str(date).split('-'):
date_dir += i
if not os.path.exists(work_path+date_dir):
to_make_dir = work_path+date_dir
os.mkdir(to_make_dir)
else:
num = 1
for f in os.listdir(work_path):
if f.startswith(date_dir):
num = num +1
to_make_dir = work_path+date_dir+"_"+str(num)
os.mkdir(to_make_dir)
return to_make_dir
def Main(host,port,user,passwd,work_path):
## Login on your email account,print login info and date time.
m = imaplib.IMAP4_SSL(port=port,host=host)
m.login(user,passwd)
print (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
print ("Login on successfully!")
# select inbox unseen emails as the target emails
m.select('INBOX',readonly=False)
email_num,unseen_data = m.search(None,'UnSeen')
unseen_list = unseen_data[0].decode("utf-8").split(' ')
count = 0
all_dir = []
for num in unseen_list:
count += 1
try:
# Trying to fetch the email.
typ,data = m.fetch(num, 'RFC822')
except:
print ("No unseen emails!")
print ("Login out now.")
m.close()
m.logout()
exit()
print ("Unread email No. ",num)
# Parsing using email lib.
msg = email.message_from_string(data[0][1].decode())
#m.store(num,'+FLAGS','\\Seen')# store this email as seen.
#Get the subject.
#subject = msg.get('subject')
sub_object = email.header.decode_header(msg['subject'])[0]
charset,sub = sub_object[1],sub_object[0]
if charset != None:
subject = sub.decode(charset)
print "Subject:",subject
# Get message body
attach = {}
for part in msg.walk():
if part.is_multipart():
continue
contentType = part.get_content_type()
mycode = part.get_content_charset()
if contentType == 'text/plain':
content = part.get_payload(decode=True).decode(charset)
print "Message:\n",content
#Get attachment
if part.get('Content-Disposition') != None:
filename = part.get_filename()
attach_name = email.Header.decode_header(filename)[0][0].decode(charset)
attach_data = part.get_payload(decode=True)
attach[attach_name] = attach_data
#Make a date dir for every email
date_dir = Make_date_dir(work_path+'/')
#Write the content of the msg
cont_file = os.path.join(date_dir+"/","Content")
if not os.path.isfile(cont_file):
with open(cont_file,"wb") as fh:
fh.write(content)
#Write the attachment
for name,data in attach.items():
att_file = os.path.join(date_dir+'/',name)
if not os.path.isfile(att_file):
with open(att_file,'wb') as fp:
fp.write(data)
m.logout()
print ("Email count:",count)
return
if __name__ == '__main__':
# server and host depend your email accounts type
# yours may be different
host = 'imap.exmail.qq.com'
port = '993'
user = raw_input("Email:")
passwd = getpass.getpass()
work_path = raw_input("Enter your work dir:")
work_path = "./"
Main(host,port,user,passwd,work_path)