By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,549 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

python, mbox files and forwarding messages

P: n/a
Can Python parse a mbox file and forward each individual message within
that file to someone else?

For example, let's say I have a 10MB mbox file that has 678 messages.
I'd like to send each of the messages to sa*********@northpole.com...
how might I do that? Also, how might I handle messages that have an
attachment associated with them during the forward process?

Thanks,
Brad
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> Can Python parse a mbox file and forward each individual message within
that file to someone else?
Yes.
For example, let's say I have a 10MB mbox file that has 678 messages.
I'd like to send each of the messages to sa*********@northpole.com...
how might I do that? Also, how might I handle messages that have an
attachment associated with them during the forward process?


import smtplib

def find_all_at_linestart(data, st):
start = 0
if data[:len(st)] == st:
start += len(st)
yield 0
end = len(data)
st = \n'+st
while start < end:
nstart = data.find(st, start, end)
if nstart == -1:
return
start = nstart+len(st)
yield nstart

def forward_email(mboxfname, email_from, email_to, host, port):
mail = open('mboxfname', 'rb').read()
lst = list(find_all_at_linestart(mail, 'From '))
smtpc = smtplib.SMTP(host, port)
for i in xrange(len(lst)-1):
start = lst[i]
end = lst[i+1]
#strip off the mbox From ... line
r = mail.find('\n', start, end)
email = mail[r+1:end]
if email:
smtpc.sendmail(email_from, [email_to], email)
start = lst[-1]
end = len(mail)
r = mail.find('\n', start, end)
email = mail[r+1:end]
if email:
smtpc.sendmail(email_from, [email_to], email)
smtpc.close()
Pardon if it doesn't quite work, this was done in my email client, and
has not been tested.

- Josiah

Jul 18 '05 #2

P: n/a
On Tue, 26 Oct 2004 13:08:36 -0400, Brad Tilley <br********@gmail.com> wrote:
Can Python parse a mbox file and forward each individual message within
that file to someone else?
[...] Also, how might I handle messages that have an
attachment associated with them during the forward process?


By ignoring them. You don't have to be MIME-aware to forward a message, and
the mbox format(s) aren't MIME-aware either.

/Jorgen

--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.