468,743 Members | 2,238 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,743 developers. It's quick & easy.

python, mbox files and forwarding messages

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
2 2929
> 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
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.

Similar topics

1 post views Thread by Paul Rubin | last post: by
7 posts views Thread by Uwe Grauer | last post: by
52 posts views Thread by Olivier Scalbert | last post: by
10 posts views Thread by Andrew Dalke | last post: by
5 posts views Thread by Gregory Piñero | last post: by
11 posts views Thread by Jonathan Pritchard | last post: by
18 posts views Thread by Grant Edwards | last post: by
reply views Thread by Steve Holden | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.