-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathmbox_send2.py
executable file
·161 lines (124 loc) · 5.34 KB
/
mbox_send2.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
#!/usr/bin/python
"""\
A command-line utility that can (re)send all messages in an mbox file
to a specific email address, with options for controlling the rate at
which they are sent, etc.
"""
# Upstream: https://gist.github.com/wojdyr/1176398#comment-1300024
# I got this script from Robin Dunn a few years ago, see
# https://github.com/wojdyr/fityk/wiki/MigrationToGoogleGroups
import sys
import os
import time
import mailbox
import email
import smtplib
from optparse import OptionParser, make_option
#---------------------------------------------------------------------------
# Set some defaults
defTo = []
defFrom = None
defChunkSize = 100
defChunkDelay = 30
defSmtpHost = 'localhost'
defSmtpPort = 25
defCount = -1
defStart = -1
# define the command line options
option_list = [
make_option('--to', action='append', dest='toAddresses', default=defTo,
help="The address to send the messages to. May be repeated."),
make_option('--from', dest='fromAddress', default=defFrom,
help="The address to send the messages from."),
make_option('--chunk', type='int', dest='chunkSize', default=defChunkSize,
help='How many messages to send in each batch before pausing, default: %d' % defChunkSize),
make_option('--pause', type='int', dest='chunkDelay', default=defChunkDelay,
help='How many seconds to delay between chunks. default: %d' % defChunkDelay),
make_option('--count', type='int', dest='count', default=defCount,
help='How many messages to send before exiting the tool, default is all messages in the mbox.'),
make_option('--start', type='int', dest='start', default=defStart,
help='Which message number to start with. Defaults to where the tool left off the last time, or zero.'),
make_option('--smtpHost', dest='smtpHost', default=defSmtpHost,
help='Hostname where SMTP server is running'),
make_option('--smtpPort', type='int', dest='smtpPort', default=defSmtpPort,
help='Port number to use for connecting to SMTP server'),
]
smtpPassword = None # implies using TLS
#---------------------------------------------------------------------------
def get_hwm(hwmfile):
if not os.path.exists(hwmfile):
return -1
hwm = int(file(hwmfile).read())
return hwm
def set_hwm(hwmfile, count):
f = file(hwmfile, 'w')
f.write(str(count))
f.close()
def main(args):
if sys.version_info < (2,5):
print "Python 2.5 or better is required."
sys.exit(1)
# Parse the command line args
parser = OptionParser(usage="%prog [options] mbox_file(s)",
description=__doc__,
version="%prog 0.9.1",
option_list=option_list)
options, arguments = parser.parse_args(args)
# ensure we have the required options
if not options.toAddresses:
parser.error('At least one To address is required (use --to)')
if not options.fromAddress:
parser.error('From address is required (use --from)')
if not arguments:
parser.error('At least one mbox file is required')
# process the mbox file(s)
for mboxfile in arguments:
print "Opening %s..." % mboxfile
mbox = mailbox.mbox(mboxfile)
totalInMbox = len(mbox)
print "Total messages in mbox: %d" % totalInMbox
hwmfile = mboxfile + '.hwm'
print 'Storing last message processed in %s' % hwmfile
start = get_hwm(hwmfile)
if options.start != -1:
start = options.start
start += 1
print 'Starting with message #%d' % start
totalSent = 0
current = start
# Outer loop continues until either the whole mbox or options.count
# messages have been sent,
while (current < totalInMbox and
(totalSent < options.count or options.count == -1)):
# Inner loop works one chunkSize number of messages at a time,
# pausing and reconnecting to the SMTP server for each chunk.
print 'Connecting to SMTP(%s, %d)' % (options.smtpHost, options.smtpPort)
smtp = smtplib.SMTP(options.smtpHost, options.smtpPort)
if smtpPassword: # use TLS
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(options.fromAddress, smtpPassword)
chunkSent = 0
while chunkSent < options.chunkSize:
msg = mbox[current]
print 'Processing message %d: %s' % (current, msg['Subject'])
# Here is where we actually send the message
smtp.sendmail(options.fromAddress, options.toAddresses, msg.as_string())
set_hwm(hwmfile, current) # set new 'high water mark'
current += 1
totalSent += 1
chunkSent += 1
if (current >= totalInMbox or
(totalSent >= options.count and options.count != -1)):
break
else:
smtp.close()
del smtp
print "Pausing for %d seconds..." % options.chunkDelay,
time.sleep(options.chunkDelay)
print
print 'Goodbye'
#---------------------------------------------------------------------------
if __name__ == '__main__':
main(sys.argv[1:])