python, mbox files and forwarding messages

B

Brad Tilley

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 (e-mail address removed)...
how might I do that? Also, how might I handle messages that have an
attachment associated with them during the forward process?

Thanks,
Brad
 
J

Josiah Carlson

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 (e-mail address removed)...
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
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
 
J

Jorgen Grahn

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top