-
Notifications
You must be signed in to change notification settings - Fork 6
/
maildir2gmail.py
executable file
·228 lines (187 loc) · 7.13 KB
/
maildir2gmail.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
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
#!/usr/bin/env python
"""Upload email messages from a list of Maildir to Google Mail."""
__version__ = '0.4'
import email
import email.Header
import email.Utils
import os
import sys
import datetime
import time
import re
from pprint import pprint
class Gmail(object):
def __init__(self, options):
self.username = options.username
self.password = options.password
self.new_flag = options.new_flag
self.folder = options.folder
self.ignore_missing_date = options.ignore_missing_date
self.max_size = options.max_size
if self.folder == 'inbox':
self.folder = 'INBOX'
else:
#self.folder = '[Gmail]/%s' % self.folder
self.folder = '%s' % self.folder
self.__database = None
self.__imap = None
def __del__(self):
if self.__database is not None:
try:
self.__database.close()
except:
pass
self.__database = None
if self.__imap is not None:
try:
self.__imap.logout()
self.__imap.close()
except:
pass
self.__imap = None
def append(self, filename):
if self.check_appended(filename):
return
content = open(filename, 'rb').read()
if content.endswith('\x00\x00\x00'):
log('Skipping "%s" - corrupted' % os.path.basename(filename))
return
message = email.message_from_string(content)
timestamp = parsedate(message['date'])
fileTimestamp = os.path.getctime(filename)
if not self.ignore_missing_date:
if not timestamp:
log('Skipping "%s" - no date (creation time of file: %s)' % (os.path.basename(filename), datetime.datetime.fromtimestamp(fileTimestamp).strftime('%Y-%m-%d %H:%M:%S')))
return
else:
timestamp = os.path.getctime(filename)
if message.is_multipart():
message_size = 0
for part in message.walk():
part_payload = part.get_payload(decode=True)
if part_payload:
message_size = message_size + len(part_payload)
else:
message_size = len(content)
subject = decode_header(message['subject'])
if message_size > self.max_size:
log('Skipping "%s" (subject: "%s") - message too large (max. 25 MB); Size was %d bytes)' % (os.path.basename(filename), subject, len(content)))
return
log('Sending "%s" (%d bytes)' % (subject, message_size))
del message
if self.new_flag:
self.imap.append(self.folder, '()', timestamp, content)
else:
self.imap.append(self.folder, '(\\Seen)', timestamp, content)
self.mark_appended(filename)
def check_appended(self, filename):
return os.path.basename(filename) in self.database
def mark_appended(self, filename):
self.database[os.path.basename(filename)] = '1'
@property
def database(self):
if self.__database is None:
import shelve
dbname = os.path.abspath(os.path.splitext(sys.argv[0])[0])
self.__database = shelve.open(dbname)
return self.__database
@property
def imap(self):
from imaplib import IMAP4_SSL
if self.__imap is None:
if not self.username or not self.password:
raise Exception('Username/password not supplied')
self.__imap = IMAP4_SSL('imap.gmail.com')
self.__imap.login(self.username, self.password)
log('Connected to Gmail IMAP')
# verify if the specified folder exists
f = self.__imap.status(self.folder, '(MESSAGES)')
if (f[0] == 'OK'):
# everything is fine, the folder exists
pass
elif (f[0] == 'NO'):
# the folder does not exist
f_p = self.folder.split('/')
f_here = ''
for f_c in f_p:
if (f_here == ''):
f_here = f_c
else:
f_here += '/' + f_c
# check if this folder exists
f2 = self.__imap.status(f_here, '(MESSAGES)')
if (f2[0] == 'NO'):
self.__imap.create(f_here)
log("Create mailbox: " + f_here)
else:
raise Exception('Internal error quering the folder status')
return self.__imap
def decode_header(value):
result = []
for v, c in email.Header.decode_header(value):
try:
if c is None:
v = v.decode()
else:
v = v.decode(c)
except (UnicodeError, LookupError):
v = v.decode('iso-8859-1')
result.append(v)
return u' '.join(result)
def encode_unicode(value):
if isinstance(value, unicode):
for codec in ['iso-8859-1', 'utf8']:
try:
value = value.encode(codec)
break
except UnicodeError:
pass
return value
def log(message):
print '[%s]: %s' % (time.strftime('%H:%M:%S'), encode_unicode(message))
def main():
from optparse import OptionParser
parser = OptionParser(
description=__doc__,
usage='%prog [options] [maildirs]',
version=__version__
)
parser.add_option('-f', '--folder', dest='folder', default='All Mail',
help='Folder to store the emails. [default: %default]')
parser.add_option('-p', '--password', dest='password',
help='Password to log into Gmail')
parser.add_option('-u', '--username', dest='username',
help='Username to log into Gmail')
parser.add_option('-n', '--new', dest='new_flag', action = 'store_true',
help='Flag all messages as UNSEEN')
parser.add_option('--ignore-missing-date', dest='ignore_missing_date', action='store_true',
help='Ignores messages with missing date and sets the file creation time instead')
parser.add_option('--max-size', dest='max_size', type="int", default=25000000,
help='Defines maximum message size [default: %default bytes]')
options, args = parser.parse_args()
# basic sanity check for folder name
if (re.match(r'^[a-zA-Z0-9 \[\]\-_\/\.&]+\Z', options.folder) is None):
raise Exception('Invalid folder name')
gmail = Gmail(options)
for dirname in args:
for filename in os.listdir(dirname):
filename = os.path.join(dirname, filename)
if os.path.isfile(filename):
try:
gmail.append(filename)
except:
log('Unable to send %s' % filename)
raise
def parsedate(value):
if value:
value = decode_header(value)
value = email.Utils.parsedate_tz(value)
if isinstance(value, tuple):
timestamp = time.mktime(tuple(value[:9]))
if value[9]:
timestamp -= time.timezone + value[9]
if time.daylight:
timestamp += 3600
return time.localtime(timestamp)
if __name__ == '__main__':
main()