-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgml.py
executable file
·92 lines (85 loc) · 3.54 KB
/
gml.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
#!/usr/bin/env python
#
# Copyright (C) 2004 Mark H. Lyon <[email protected]>
#
# This file is the Mbox & Maildir to Gmail Loader (GML).
#
# Mbox & Maildir to Gmail Loader (GML) is free software; you can redistribute
# it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# GML is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with GML; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Origional development thread at Ars Technica:
# http://episteme.arstechnica.com/eve/ubb.x?a=tpc&s=50009562&f=6330927813&m=108000474631
#
# Version 0.1 - 15 Jun 04 16:28 Supports Mbox
# Version 0.2 - 15 Jun 04 18:48 Implementing Magus` suggestion for Maildir
# Version 0.3 - 16 Jun 04 16:17 Implement Rold Gold suggestion for counters
# Version 0.4 - 17 Jun 04 13:15 Add support for changing SMTP server at command line
# Version 0.5 - 05 Oct 09 redo exception handling to see what Google's
# complaints are on failure, update to use TLS
import mailbox, smtplib, sys, time, string
def main ():
print "\nMbox & Maildir to Gmail Loader (GML) by Mark Lyon <[email protected]>\n"
if len(sys.argv) in (5, 6) :
boxtype_in = sys.argv[1]
mailboxname_in = sys.argv[2]
emailname_in = sys.argv[3]
password_in = sys.argv[4]
else:
usage()
try:
smtpserver_in = sys.argv[5]
except:
smtpserver_in = 'smtp.gmail.com'
print "Using smtpserver %s\n" % smtpserver_in
count = [0,0,0]
try:
if boxtype_in == "maildir":
mb = mailbox.Maildir(mailboxname_in)
else:
mb = mailbox.UnixMailbox (file(mailboxname_in,'r'))
msg = mb.next()
except:
print "*** Can't open file or directory. Is the path correct? ***\n"
usage()
while msg is not None:
try:
document = msg.fp.read()
except:
count[2] = count[2] + 1
print "*** %d MESSAGE READ FAILED, SKIPPED" % (count[2])
msg = mb.next()
if document is not None:
fullmsg = msg.__str__( ) + '\x0a' + document
server = smtplib.SMTP(smtpserver_in)
#server.set_debuglevel(1)
server.ehlo()
server.starttls()
# smtplib won't send auth info without this second ehlo after
# starttls -- thanks to
# http://bytes.com/topic/python/answers/475531-smtplib-authentication-required-error
# for the tip
server.ehlo()
server.login(emailname_in, password_in)
server.sendmail(msg.getaddr('From')[1], emailname_in, fullmsg)
server.quit()
count[0] = count[0] + 1
print " %d Forwarded a message from: %s" % (count[0], msg.getaddr('From')[1])
msg = mb.next()
print "\nDone. Stats: %d success %d error %d skipped." % (count[0], count[1], count[2])
def usage():
print 'Usage: gml.py [mbox or maildir] [mbox file or maildir path] [gmail address] [gmail password] [Optional SMTP Server]'
print 'Exmpl: gml.py mbox "c:\mail\Inbox" [email protected] password'
print 'Exmpl: gml.py maildir "c:\mail\Inbox\" [email protected] password gsmtp171.google.com\n'
sys.exit()
if __name__ == '__main__':
main ()