/
mailHandle.py
202 lines (168 loc) · 6.86 KB
/
mailHandle.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
'''
mailHandle
mailHandle.Email(account[, password][, friends])
account and password are string(users email account and password)
friends is List
mailHandle.setFriends([friends])
friends is List with value string(friend name)
set users friends which want to getEmail()
mailHandle.login([account[, password]])
account and password are string(users email account and password)
should use login() and Success before sendMailSmtp() and getEmail(),
Otherwise it will not be executed
it will return Boolean to tell Login status
mailHandle.sendMailSmtp(recipient, subject, content)
recipient, subject and content are string
recipient is email recipient
subject is email subject
content is email content
it will return Boolean to tell successful or not
mailHandke.getEmail()
it will return dictionary with keys are friend and value is a List
the one of List value is a email data
the data is a tuple (strFrom, strSubject, strContent, strDate, strsSuffix)
it will return None if connection error
'''
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import imaplib
import sys
import email, email.header
import email.charset
import xml.etree.ElementTree
class Email:
def __init__(self, account='', password='', friends=[]):
self.account = account
self.password = password
if account != '':
self.sHost, self.sPort, self.iHost, self.iPort = self.getServer()
self.friends = friends
self.isLogin = False
def setFriends(self, friends=[]):
self.friends = friends
def login(self, account=None, password=None):
if account:
self.account = account
if password:
self.password = password
self.sHost, self.sPort, self.iHost, self.iPort = self.getServer()
try:
mailServer = smtplib.SMTP_SSL(self.sHost, self.sPort)
mailServer.set_debuglevel(0)
mailServer.login(self.account, self.password)
self.isLogin = True
return True
except:
self.isLogin = False
return False
def getServer(self):
temp = self.account.split('@', 1)
if len(temp) != 1:
temp = temp[1].split('.')[0]
if temp == 'gmail':
return 'smtp.gmail.com', 465, 'imap.gmail.com', 993
elif temp == 'yahoo':
return 'smtp.mail.yahoo.com', 465, 'imap.mail.yahoo.com', 993
return 'undefined', 0, '', 0
def sendMailSmtp(self, recipient, subject, content):
if self.isLogin:
message = MIMEMultipart()
message['From'] = self.account
message['To'] = recipient
message['Subject'] = subject
message.attach(MIMEText(content))
mailServer = smtplib.SMTP_SSL(self.sHost, self.sPort)
mailServer.set_debuglevel(1)
mailServer.login(self.account, self.password)
mailServer.sendmail(self.account, recipient, message.as_string())
mailServer.close()
return True
return False
def getEmail(self):
mailDict = {}
try:
if self.isLogin:
if len(self.friends) > 0:
imapServer = imaplib.IMAP4_SSL(self.iHost, self.iPort)
imapServer.login(self.account, self.password)
imapServer.select()
for friend in self.friends:
#mailDict[friend] = []
criterionFrom = '(FROM ' + '\"' + friend + '\")'
typ, msgnums = imapServer.search(None, criterionFrom, 'UNSEEN')
#typ, msgnums = imapServer.search(None, criterionFrom)
isfirst=True
for num in msgnums[0].split():
typ, data = imapServer.fetch(num, '(RFC822)')
if isfirst:
isfirst = False
mailDict[friend] = []
mailDict[friend].append(self.parsingMail(data[0][1]))
imapServer.close()
imapServer.logout()
return mailDict
except:
print('getEmail error')
return None
return mailDict
def unicode(self, text, encoding=None):
if encoding == None:
return text
return text.decode(encoding)
def parsingMail(self, data):
message = email.message_from_bytes(data, _class=email.message.Message)
temp = message['From'].split(' ')
if len(temp) == 2:
fromname = email.header.decode_header(temp[0].strip('\\'))
strFrom = self.unicode(fromname[0][0], fromname[0][1]) +' '+ temp[1]
else:
strFrom = message['From']
subject = email.header.decode_header(message['Subject'])
strSubject = self.unicode(subject[0][0], subject[0][1])
mailContent = ''
contenttype = None
suffix = ''
for part in message.walk():
if not part.is_multipart():
contenttype = part.get_content_type()
filename = part.get_filename()
charset = part.get_content_charset()
if filename: #is annex?
print(filename)
else:
if charset == None:
mailContent = part.get_payload()
else:
mailContent = part.get_payload(decode=True).decode(charset)
if contenttype in ['text/plain']:
suffix = '.txt'
elif contenttype in ['text/html']:
suffix = '.htm'
mailContent = self.remove_tags(mailContent)
return (strFrom, strSubject, mailContent, message['Date'], suffix)
def remove_tags(self, text):
return ''.join(xml.etree.ElementTree.fromstring(text).itertext())
if __name__ == '__main__':
account = 'bbai14915@gmail.com'
#account = 'bbai1gmail.com'
password = 'zxc09876'
recipient = 'pe83aa517z@gmail.com'
subject = 'test python'
content = 'YAPYAPYAP22\n'
friends = ['pe83aa517z@gmail.com', 'pe83aa517z@yahoo.com.tw']
print('Start...')
#e = Email(account, password, friends)
e = Email()
print(str(e.login(account, password)))
#e.login()
e.sendMailSmtp(recipient, subject, content)
e.setFriends(friends)
mailDict = e.getEmail()
for list in mailDict.keys():
for mess in mailDict[list]:
for string in mess:
print(string)
print()
print('end')