473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 3123
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul Rubin | last post by:
I was surprised there was no obvious way with spamassassin (maybe I shoulda looked at spambayes) to split an existing mbox file into its spam and non-spam messages. So I wrote one. It's pretty...
7
by: Uwe Grauer | last post by:
Hi all, who can help on reading and writing mozilla-mail folders with python? I wanted to implement something like an email-sync for mozilla-mail in python. I did some tests, but something is...
52
by: Olivier Scalbert | last post by:
Hello , What is the python way of doing this : perl -pi -e 's/string1/string2/' file ? Thanks Olivier
4
by: Chuck Amadi | last post by:
Has anyone got a simple python script that will parse a linux mbox and create a large file to view . Cheers Chu
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
5
by: Gregory Piñero | last post by:
I was wondering what methods you experts would reccomend for this task? Here are the options I have come up with so far: 1. Build something with the poblib library...
11
by: Jonathan Pritchard | last post by:
Does anyone know of a library to read/write mbox format (used to store emails e.g. in Thunderbird) in ANSI C (cross platform is key)? Is there a more appropriate place to ask this? -- Reclaim...
18
by: Grant Edwards | last post by:
Could whoever is responsible for the gateway that is grabbing my postings off of Usenet and e-mailing them out please fix the headers in the mail messages so that I don't get the bounce messages?...
0
by: Steve Holden | last post by:
Sirshendu Rakshit wrote: I believe the difference between UnixMailbox and other types is that the UnixMailbox is specifically designed to identify the gaps between messages by the blank line and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.