473,396 Members | 1,599 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,396 software developers and data experts.

Sorting Unix mailboxes

I'm writing a program in python to sort the mail in standard Unix
email boxes. In my "prof of concept" example I am coping a letter to a
second mailbox if the letter was send from a particular email
address. When I read the destination mailbox with cat, I can see that
something is getting copied to it, but the mail program does not
recognize any new letters in the destination mailbox. It would seam
that the "OutFile.write(Message.get_unixfrom())" line is
essential. However if I run with this line uncommented then I get an
the following error. "TypeError: argument 1 must be string or
read-only character buffer, not None". I created this program by
following an example posted somewhere on the Internet, that I can't
seam to find anymore. At one time I was able to get Python to put new
letters in a mailbox.

Also, I was wondering is there were a way to use Python to delete items
from a mailbox. I could create I temp box of non-deleted then copy to
the source box, but that seams messy.

Here is my example program..
def CopyToBox(Source,Address,Destination):
AddressRE=re.compile(
"([a-zA-Z0-9._-]+)@([a-zA-Z0-9._-]+)\.([a-zA-Z0-9]+)")
InFile = open("/home/stevef/mail/%s" % Source)
OutFile = open("/home/stevef/mail/%s" % Destination,"a")
Box = mailbox.PortableUnixMailbox(InFile)
Envelope=Box.next()
while 1:
Envelope=Box.next()
if Envelope == None:
break
print Envelope.getallmatchingheaders("from")[0]
Match=AddressRE.search(
Envelope.getallmatchingheaders("from")[0])
if Match:
Set=Match.groups()
if "%s@%s.%s" % Set == Address:
print "Copy letter from %s@%s.%s" % Set
Message = email.message_from_file(Envelope.fp)
#OutFile.write(Message.get_unixfrom()) ##error
OutFile.write("\n")
OutFile.write(Message.as_string())
InFile.close()
OutFile.close()
return

Sep 13 '05 #1
3 1894
[posted and mailed, in case the OP has given up on reading the group!]

On Tue, 13 Sep 2005, sf***@io.com wrote:
I'm writing a program in python to sort the mail in standard Unix
email boxes. In my "prof of concept" example I am coping a letter to a
second mailbox if the letter was send from a particular email
address. When I read the destination mailbox with cat, I can see that
something is getting copied to it, but the mail program does not
recognize any new letters in the destination mailbox. It would seam
that the "OutFile.write(Message.get_unixfrom())" line is
essential.
Absolutely! The From line is the key element in mailbox structure.
However if I run with this line uncommented then I get an the following
error. "TypeError: argument 1 must be string or read-only character
buffer, not None".
This is happening because Message.get_unixfrom is returning None, rather
than a proper From line. According to its documentation, thus method
"defaults to None if the envelope header was never set". Since you've
never set the envelope header, this behaviour is therefore not surprising.
But didn't the envelope header get set when you created the message?
Actually, no - you created it with "email.message_from_file(Envelope.fp)",
which reads the contents of the email from the file Envelope.fp.
Envelope.fp, however, isn't the complete text of the mailbox entry, it's
just (AFAICT) the payload of the message. Therefore, the message you
create has no headers or envelope, just the body.
I created this program by following an example posted somewhere on the
Internet, that I can't seam to find anymore. At one time I was able to
get Python to put new letters in a mailbox.

Also, I was wondering is there were a way to use Python to delete items
from a mailbox.
Not really. This is a universal problem which affects all programs,
regardless of language, with work with file formats consisting of
variable-sized records - there's no wasy way to delete them.
I could create I temp box of non-deleted then copy to the source box,
but that seams messy.
A cleaner way would be to copy the non-deleted messages to a new file,
then to throw away the old file and rename the new one to replace it. This
would avoid the second copy. Alternatively, you could read and write
simultaneously with one file, then truncate at the end; this takes a bit
more care, though.
Here is my example program..
Right. Some of this makes sense to me, but there's quite a lot here that i
don't get. Perhaps some of this is a result of the code being excised from
its natural context, though.
def CopyToBox(Source,Address,Destination):
AddressRE=re.compile(
"([a-zA-Z0-9._-]+)@([a-zA-Z0-9._-]+)\.([a-zA-Z0-9]+)")
Why did you write the regexp to capture the address as three groups? It
seems like the only thing you ever do with the groups is put them back
together again!

Also, it's better to define the regexp once, at global scope, to avoid
having to compile it every time the function runs.
InFile = open("/home/stevef/mail/%s" % Source)
OutFile = open("/home/stevef/mail/%s" % Destination,"a")
Box = mailbox.PortableUnixMailbox(InFile)
Envelope=Box.next()
Why 'Envelope'? That object is a Message, not an Envelope!

And did you really mean to throw away the first message in the box like
this?
while 1:
Envelope=Box.next()
if Envelope == None:
break
Why an infinite loop with a break and an explicit next call? Why not a for
loop over the mailbox?
print Envelope.getallmatchingheaders("from")[0]
Match=AddressRE.search(
Envelope.getallmatchingheaders("from")[0])
Why getallmatchingheaders("from")[0] rather than
getfirstmatchingheader["from"]?
if Match:
Set=Match.groups()
if "%s@%s.%s" % Set == Address:
print "Copy letter from %s@%s.%s" % Set
Message = email.message_from_file(Envelope.fp)
Message now contains the email's payload, but not its headers or envelope
details, so ...
#OutFile.write(Message.get_unixfrom()) ##error
That doesn't work.
OutFile.write("\n")
OutFile.write(Message.as_string())
InFile.close()
OutFile.close()
return


There's no need for an explicit return here.

I have to sympathise with you over python's mail-handling libraries,
though; having both the rfc822 and email modules around at the same time
is quite a headache. Luckily, there's a way to make things simpler and
much easier to work with, using a trick described in the docs for the
mailbox module: rather than letting the mailbox module make the message
objects (using the rfc822 module to do it), we can supply our own message
factory function, with which we can create email-module messages right
from the start. You need a function like this:

def msgfactory(f):
while True:
try:
return email.message_from_file(f)
except:
pass

Then you can make a mailbox like this:

mbox = mailbox.PortableUnixMailbox(f, msgfactory)

The messages in it will then be email.Message instances.

I'd then write the main function like this (you'll need to import
os.path):

MBOX_DIR = "/home/stevef/mail"

def CopyToBox(src, addr, dst):
in_ = file(os.path.join(MBOX_DIR, src))
out = file(os.path.join(MBOX_DIR, dst), "a")
for mail in mailbox.PortableUnixMailbox(in_, msgfactory):
if (addr in mail["from"]):
out.write(mail.as_string(True))
in_.close()
out.close()

Simple, eh?

tom

--
Also, a 'dark future where there is only war!' ... have you seen the news lately? -- applez
Sep 15 '05 #2
sf***@io.com <sf***@io.com> wrote:
I'm writing a program in python to sort the mail in standard Unix
email boxes. In my "prof of concept" example I am coping a letter to a
second mailbox if the letter was send from a particular email
address. When I read the destination mailbox with cat, I can see that
something is getting copied to it, but the mail program does not
recognize any new letters in the destination mailbox. It would seam
that the "OutFile.write(Message.get_unixfrom())" line is
essential. However if I run with this line uncommented then I get an
the following error. "TypeError: argument 1 must be string or
read-only character buffer, not None". I created this program by
following an example posted somewhere on the Internet, that I can't
seam to find anymore. At one time I was able to get Python to put new
letters in a mailbox.

Also, I was wondering is there were a way to use Python to delete items
from a mailbox. I could create I temp box of non-deleted then copy to
the source box, but that seams messy.


Before writing Python script, perhaps, you should look at
man procmailrc
man formail
and take the relevant process and implement that in Python.

--
William Park <op**********@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
Sep 16 '05 #3
On Tue, Sep 13, 2005 at 09:23:35AM -0700, sf***@io.com wrote:
I'm writing a program in python to sort the mail in standard Unix
email boxes. In my "prof of concept" example I am coping a letter to a
second mailbox if the letter was send from a particular email
address. [...] Also, I was wondering is there were a way to use Python to delete items
from a mailbox.


As part of Google's Summer of Code program, I wrote a replacement for
the mailbox module that would be well suited for this. It might be
included in the Python 2.5 distribution. Until then, you could get the
module from Python CVS, under nondist/sandbox/mailbox.

More info is on the project Web site: http://gkj.freeshell.org/soc/

--
Gregory K. Johnson
Sep 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jed Parsons | last post by:
What headers to I have to know about to build thread trees from Unix mailboxes? Is it enough to get the In-Reply-To header for each message and build a dictionary of { Message-ID: message }...
13
by: Paul | last post by:
Hi all I have a sorting problem, but my experience with Python is rather limited (3 days), so I am running this by the list first. I have a large database of 15GB, consisting of 10^8 entries...
1
by: Eric S. Johansson | last post by:
I've been searching around for the equivalent to the mailbox module except with the capability of writing messages as well as reading. If it makes it easier, I only need to write to maildir...
0
by: KJemison | last post by:
I have an initial setup of Postfix pushing all mail from a domain to Dbmail using dbmail-lmtp. There are no mailboxes setup on Postfix. How or what program is going to sort the messages into each...
4
by: Gareth Gale | last post by:
I'm trying to implement a way of allowing a user to sort a HTML table via Javascript on the client. I've seen lots of samples where single column sorting (asc or desc) is shown, but I'd like nested...
2
by: Jack | last post by:
hi all I would find a parallel sorting algorithms written with pthread programs, as a benchmark to run. Any pointing will be of help thankyou in advance
4
by: Ron Vecchi | last post by:
I a runnning w2k3 pop3 mail server that came with iis6. I would like to write an application that progammtically creates the new mailboxes in an already established mail domain. Does anyone know...
0
by: Yashesh Bhatia | last post by:
hello, i'm implementing an imap based mail client and want to provide sorting / paging functionality for the inbox. there's a function to retrieve headers imap_headers (http://...
12
by: Ira.Kovac | last post by:
Hello all, I have an Unicode text file with 1.6 billon lines (~2GB) that I'd like to sort based on first two characters. I'd greatly appreciate if someone can post sample code that can help...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.