-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_util.py
78 lines (69 loc) · 2.7 KB
/
email_util.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import imaplib
from pyzmail import *
from envelopes import Envelope
import pdb
class EmailUtil:
def __init__(self, server ="", username="", password=""):
self.server = server
self.username = username
self.password = password
def letter(self, subject, content, To, files):
if isinstance(To, basestring):
To = [To]
mail = MIMEMultipart()
mail['Subject'] = Header(subject, 'utf-8')
mail['From'] = self.username
mail['To'] = ';'.join(To)
mail['Date'] = formatdate()
text = MIMEText(content, 'html', 'utf-8')
mail.attach(text)
for f in files:
part = MIMEText(open(f, 'rb').read(), 'base64', 'utf-8')
part['Content-Type'] = 'application/octet-stream'
part['Content-Disposition'] = 'attachment; filename="%s"'%f
mail.attach(part)
return mail.as_string()
def sendEmail(self, toAddrList, subject, content='', files=[]):
mail = Envelope(toAddrList, self.username, subject, content)
for f in files:
mail.add_attachment(f)
try:
mail.send('smtp.'+self.server, login=self.username, password=self.password, tls=True)
except Exception as e:
print e
def recvEmail(self, criterion = 'Unseen'):
result = []
try:
imap = imaplib.IMAP4_SSL('imap.'+self.server)
imap.login(self.username, self.password)
imap.select('INBOX')
resp, items = imap.search(None, criterion)
for i in items[0].split():
pdb.set_trace()
typ, content = imap.fetch(i, '(RFC822)')
msg = PyzMessage.factory(content[0][1])
imap.store(i, '+FLAGS', '\\seen')
res = {
'from': msg.get_address('from')[1],
'cc': [x[1] for x in msg.get_addresses('cc')],
'subject': msg.get_subject(),
'content':[]
}
for part in msg.mailparts:
if part.is_body:
data = part.get_payload().decode(part.charset)
res['content'].append(data)
else:
filename = part.filename
data = part.get_payload()
res['content'].append({'filename':filename, 'data':data})
result.append(res)
except Exception as e:
print e
return result
if __name__ == "__main__":
mail = EmailUtil('sina.com', '[email protected]', 'YDC21415926')
result = mail.recvEmail()
print result