-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmailer.py
145 lines (115 loc) · 4.29 KB
/
mailer.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
import getpass
import imaplib
import keyring
import smtplib
from email import Encoders
from email import message_from_string
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from settings.secure_settings import *
import BeautifulSoup
import HTMLParser
import textwrap
import os
class Mailer(object):
def __init__(self, user=None, passwd=None):
self.gmail_user = user
self.gmail_passwd = passwd
def login(self, user, passwd=None, stay_logged_in=True):
self.gmail_user = user
if passwd is None:
passwd = keyring.get_password('shh-mailer', self.gmail_user)
if passwd is None:
passwd = getpass.getpass('Password for %s: ' % self.gmail_user)
self.gmail_passwd = passwd
if stay_logged_in:
keyring.set_password('shh-mailer', self.gmail_user, self.gmail_passwd)
def logout(self):
keyring.delete_password('shh-mailer', self.gmail_user)
self.gmail_user = None
self.passwd = None
def mail(self, to, subject, text, attach=None):
if self.gmail_user is None:
self.login(DEFAULT_EMAIL)
msg = MIMEMultipart()
msg['From'] = self.gmail_user
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text))
if attach:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(self.gmail_user, self.gmail_passwd)
mailServer.sendmail(self.gmail_user, to, msg.as_string())
mailServer.close()
def check_mail(self):
if self.gmail_user is None:
self.login(DEFAULT_EMAIL)
# TODO(Bieber): Try out 'with' semantics
M = imaplib.IMAP4_SSL('imap.gmail.com')
M.login(self.gmail_user, self.gmail_passwd)
M.select(readonly=1)
rv, data = M.search(None, '(UNSEEN)')
email_ids = data[0].split(' ')
messages = []
for num in reversed(email_ids):
typ, data = M.fetch(num, '(RFC822)')
msg = message_from_string(data[0][1])
messages.append(Message(msg))
M.close()
return messages
class Message(object):
def __init__(self, message):
self.message = message
def subject(self):
return self.message['Subject']
def sender(self):
return self.message['From']
def date_str(self):
return self.message['Date']
def text(self):
msg = self.message
h = HTMLParser.HTMLParser()
for part in msg.walk():
content_type = part.get_content_type()
if content_type == 'text/plain':
message = str(part.get_payload())
message = message.replace('=C2=A0', ' ') # non-breaking space
message = message.replace('=E2=80=99', "'") # Apostrophe
print 'plain: """{}"""'.format(message)
elif content_type == 'text/html':
body = BeautifulSoup.BeautifulSoup(part.get_payload())
p = element_find("p", body)
font = element_find("font", body)
div = element_find("div", body)
synth_msg = textwrap.wrap(synthesize_elements(p, font, div))
message = h.unescape(' '.join(synth_msg))
message = message.replace('= ', ' ') # non-breaking space
print 'html: """{}"""'.format(message)
# TODO(Bieber): Shorten URLs to just domain name
return message
# Helper functions for parsing body of email
def element_find(element, body):
try:
element = body.findAll(element)[0].text
except IndexError:
element = None
return element
def synthesize_elements(*arg):
message = []
for i in range(len(arg)):
if arg[i]:
message.append(arg[i] + '. ')
if not message:
message = 'Undefinable'
else:
message = ', '.join(message)
return message