-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmail_wrapper.py
More file actions
268 lines (211 loc) · 9.29 KB
/
gmail_wrapper.py
File metadata and controls
268 lines (211 loc) · 9.29 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 3 13:51:07 2016
@author: Daniel Koohmarey
@company: Pericror
Copyright (c) Pericror 2016
Notes:
Run python gmail_wrapper.py --noauth_local_webserver to set up credentials on external machine
Dependencies:
sudo pip install --upgrade google-api-python-client
Resources used:
https://developers.google.com/gmail/api/quickstart/python
"""
import httplib2
import os
import argparse
import base64
import email
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from apiclient import discovery, errors
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
class GmailWrapper(object):
SCOPES = 'https://www.googleapis.com/auth/gmail.modify'
CLIENT_SECRET_FILE = 'client_secret.json' # https://console.developers.google.com/apis/credentials
APPLICATION_NAME = "Pericror Custom App Creator"
AUTHORIZED_FROM = 'info@pericror.com'
SENDER = 'pericror@gmail.com'
RECIPIENT = 'results@pericror.com'
def __init__(self):
self.credentials = self.get_credentials()
self.http = self.credentials.authorize(httplib2.Http())
self.service = discovery.build('gmail', 'v1', http=self.http)
def refresh_credentials(self, expires_in = 600):
"""Checks if the auth credentials expire soon, and requests new ones if needed
Args:
expires_in: The number of seconds in the future to check for expiration
Returns:
datetime, the current credential expiration time
"""
if self.credentials.get_access_token().expires_in <= expires_in:
self.credentials.refresh(self.http)
return self.credentials.get_access_token().expires_in
def get_credentials(self):
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'gmail-python-wrapper.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(self.CLIENT_SECRET_FILE, self.SCOPES)
flow.user_agent = self.APPLICATION_NAME
credentials = tools.run_flow(flow, store,flags)
print 'Storing credentials to ' + credential_path
return credentials
def get_unread_message_id(self):
"""Retrieve the first unread email available.
Returns:
The message id of the unread email if it exists, otherwise None.
"""
msg_id = None
try:
query = "from:{} is:unread".format(self.AUTHORIZED_FROM)
response = self.service.users().messages().list(userId='me', maxResults=1,
q=query).execute()
if 'messages' in response:
msg_id = response['messages'][0]['id']
except errors.HttpError, error:
print 'An error occurred: %s' % error
return msg_id
def get_message_data(self, msg_id):
"""Retrieve data from a given email.
Args:
msg_id: The id of the email message to retrieve the body from.
Returns:
The email body.
"""
message_data = {}
try:
message = self.service.users().messages().get(userId='me', id=msg_id).execute()
headers = {}
for header in message['payload']['headers']:
headers[header['name']] = header['value']
message_data['headers'] = headers
if 'multipart' in message['payload']['mimeType']:
for part in message['payload']['parts']:
if part['mimeType'] == 'text/plain':
data = part['body']['data']
break
else:
data = message['payload']['body']['data']
message_data['body'] = base64.urlsafe_b64decode(data.encode('ASCII'))
except Exception as e:
print 'An error occurred: %s' % e
return message_data
def mark_as_read(self, msg_id):
"""Mark an email as read.
Args:
msg_id: The id of the email message to mark as read.
Returns:
A boolean indicating message has been marked as read successfully.
"""
success = False
try:
msg_labels = { "removeLabelIds": ["UNREAD"] }
message = self.service.users().messages().modify(userId='me', id=msg_id,
body=msg_labels).execute()
success = message
except errors.HttpError, error:
print 'An error occurred: %s' % error
return success
def create_message(self, subject, plain, html):
# Returns a message object addressed to self.RECIPIENT
return self.create_message(self.RECIPIENT, subject, plain, html)
def create_message(self, to, subject, plain, html, attachment = None):
"""Create a message for an email.
Args:
to: The destination of the email message.
subject: The subject of the email message.
plain: The text of the email message.
html: The html of the email message.
attachment: The path to the attachment.
Returns:
An object containing a base64url encoded email object.
"""
message = MIMEMultipart('mixed') #https://stackoverflow.com/questions/3902455/mail-multipart-alternative-vs-multipart-mixed
message['To'] = to
message['From'] = self.SENDER
message['Subject'] = subject
message_text = MIMEMultipart('alternative')
plain_part = MIMEText(plain, 'plain')
message_text.attach(plain_part)
html_part = MIMEText(html, 'html')
message_text.attach(html_part)
message.attach(message_text)
if attachment:
content_type, encoding = mimetypes.guess_type(attachment)
msg = None
if content_type is None or encoding is not None:
content_type = 'application/octet-stream'
main_type, sub_type = content_type.split('/', 1)
if main_type == 'text':
fp = open(attachment, 'rb')
msg = MIMEText(fp.read(), _subtype=sub_type)
fp.close()
elif main_type == 'image':
fp = open(attachment, 'rb')
msg = MIMEImage(fp.read(), _subtype=sub_type)
fp.close()
elif main_type == 'audio':
fp = open(attachment, 'rb')
msg = MIMEAudio(fp.read(), _subtype=sub_type)
fp.close()
else:
fp = open(attachment, 'rb')
msg = MIMEBase(main_type, sub_type)
msg.set_payload(fp.read())
fp.close()
filename = os.path.basename(attachment)
msg.add_header('Content-Disposition', 'attachment', filename=filename)
message.attach(msg)
return {'raw': base64.urlsafe_b64encode(message.as_string())}
def send_message(self, message):
"""Send an email message.
Args:
message: Message to be sent.
Returns:
Sent message.
"""
success = None
try:
message = (self.service.users().messages().send(userId='me',
body=message).execute())
success = message
except errors.HttpError, error:
print 'An error occurred: %s' % error
return success
def test():
wrapper = GmailWrapper()
# Get an unread (unprocessed) email
unread_msg_id = wrapper.get_unread_message_id()
if unread_msg_id:
# Process the email
msg_data = wrapper.get_message_data(unread_msg_id)
msg_body = msg_data['body']
headers = msg_data['headers']
print "Processing message from: " + headers['From']
plain = "Body: {}".format(msg_body)
html = "<h3>Body:</h3>{}".format(msg_body)
# Send an email response the contains the original email
message = wrapper.create_message(headers['From'],
'Re: ' + headers['Subject'], plain, html)
wrapper.send_message(message)
# Mark the message as read so we don't process it again
wrapper.mark_as_read(unread_msg_id)
print "Sent email copy to " + headers['From']
if __name__ == '__main__':
if 'test' in sys.argv:
test()