473,804 Members | 3,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_uni xfrom())" 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(Sourc e,Address,Desti nation):
AddressRE=re.co mpile(
"([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.Portabl eUnixMailbox(In File)
Envelope=Box.ne xt()
while 1:
Envelope=Box.ne xt()
if Envelope == None:
break
print Envelope.getall matchingheaders ("from")[0]
Match=AddressRE .search(
Envelope.getall matchingheaders ("from")[0])
if Match:
Set=Match.group s()
if "%s@%s.%s" % Set == Address:
print "Copy letter from %s@%s.%s" % Set
Message = email.message_f rom_file(Envelo pe.fp)
#OutFile.write( Message.get_uni xfrom()) ##error
OutFile.write(" \n")
OutFile.write(M essage.as_strin g())
InFile.close()
OutFile.close()
return

Sep 13 '05 #1
3 1918
[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_uni xfrom())" 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_uni xfrom 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(Envel ope.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(Sourc e,Address,Desti nation):
AddressRE=re.co mpile(
"([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.Portabl eUnixMailbox(In File)
Envelope=Box.ne xt()
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.ne xt()
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.getall matchingheaders ("from")[0]
Match=AddressRE .search(
Envelope.getall matchingheaders ("from")[0])
Why getallmatchingh eaders("from")[0] rather than
getfirstmatchin gheader["from"]?
if Match:
Set=Match.group s()
if "%s@%s.%s" % Set == Address:
print "Copy letter from %s@%s.%s" % Set
Message = email.message_f rom_file(Envelo pe.fp)
Message now contains the email's payload, but not its headers or envelope
details, so ...
#OutFile.write( Message.get_uni xfrom()) ##error
That doesn't work.
OutFile.write(" \n")
OutFile.write(M essage.as_strin g())
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_f rom_file(f)
except:
pass

Then you can make a mailbox like this:

mbox = mailbox.Portabl eUnixMailbox(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.jo in(MBOX_DIR, src))
out = file(os.path.jo in(MBOX_DIR, dst), "a")
for mail in mailbox.Portabl eUnixMailbox(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_uni xfrom())" 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**********@y ahoo.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
2255
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 } pairs? Or is it more complicated than that? If there isn't already a module to do this (and apologies if there is one and I don't know about it), are the current tools of choice the 'email' and 'mailbox' modules? (And I guess I'd want to use the...
13
4638
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 of approximately 100 bytes each. I devised a relatively simple key map on my database, and I would like to order the database with respect to the key.
1
1233
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 mailboxes. I found a reference to http://pythonms.sf.net/ Python mail system) but it seems to have vanished. Any suggestions? ---eric
0
1276
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 users mailbox once they arrive from the internet? Sorry, but I am a little confused about the process. Thank you. Kell Jemison
4
2832
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 sorting i.e. sort by col1 asc, then by col2 desc etc. Can anyone help ?
2
3245
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
2737
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 how I would access this funcationality? Thanks, Ron Vecchi
0
1186
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:// www.php.net/imap_headers) however, this gets headers for all messages in a mailbox(inbox). is there any function that will fetch headers similar to a SQL LIMIT clause (LIMIT offset, row_count) - so something like fetch 10 headers
12
3935
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 me do this. Also, any ideas on approximately how long is the sort process going to take (XP, Dual Core 2.0GHz w/2GB RAM).
0
9707
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9585
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6856
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.