472,805 Members | 894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

maildir->mbox conversion script review

Hi,

I am writing a script to convert couple of thousand emails (in couple
of hundred folders) and before I will get to the hard part -- maintaing
structure folders and subfolders, and maintaing record of the status of
the message, I would like to be sure that I have at least maildir->mbox
conversion right. Could anybody comment on the below shown code please?
Thanks a lot

Matěj

---------------------------------------------------------------------------------------------------------------------
#!/usr/bin/env python
"""mdir2mbx: yet another maildir -mbox converter

mdir2mbx [maildirName] [mboxName]

TODO:
* convert all (or as many as possible) status flags from KMail
to Thunderbird.
* testing, testing, testing
"""
__version__ = "$Revision: 1.2 $"
__author__ = "Matej Cepl <mc***@redhat.com>"
__copyright__ = "(C) 2007 Matej Cepl. MIT/X11."
__date__ = "$Date: 2007/01/08 23:56:29 $"
___contributors__ = []

import email, email.Errors, email.Header, email.Generator, mailbox
import codecs, sys, cStringIO

class Mailbox(mailbox.UnixMailbox):
def __init__(self,filename):

mailbox.UnixMailbox.__init__(self,filename,email.m essage_from_file)
self.boxname=filename
self.content = ""

def add(self,msg):
fp = cStringIO.StringIO()
g = email.Generator.Generator(fp, mangle_from_=True,
maxheaderlen=65)
g.flatten(msg,unixfrom=True)
self.content += "%s\n\n" % fp.getvalue()

def write(self):
outfile=file(self.boxname,"wb")
outfile.write("%s\n" % self.content)
outfile.close()

class MyMaildir(mailbox.Maildir):
def __init__(self,dirname):
mailbox.Maildir.__init__(self,dirname,email.messag e_from_file)
self.dirname = dirname
self.decfunc = email.Header.decode_header
self.msg = ""

def __translateHeader(self,headerName):
header = email.Header.decode_header(self.msg[headerName])
string = header[0][0]
encoding = header[0][1]
if not(encoding):
encoding = "ascii"
outstr = string.decode(encoding,'ignore')
return outstr

def listHeaders(self):
for self.msg in self:
hdrfrom = self.__translateHeader("From")
#hdrto = self.__translateHeader("To")
hdrdate = self.__translateHeader("Date")
hdrsubject = self.__translateHeader("Subject")
print "%s;%s;%s" % (hdrfrom,hdrdate,hdrsubject)
#header =
email.Header.decode_header(self.msg["Message-Id"])[0][0]
#print "%s;%s" % (self.dirname,header)

def writeMBox(self,filename):
mbox = Mailbox(filename)
for self.msg in self:
mbox.add(self.msg)
mbox.write()

if __name__=="__main__":
obj = MyMaildir(sys.argv[1])
obj.writeMBox(sys.argv[2])

Jan 10 '07 #1
0 1577

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

Similar topics

1
by: Julia Goolia | last post by:
hello everyone, i have the following problem. I am trying to create maildir directories on the local filesystem then chmod, chown them to courier:courier (for courier mail server). the courier...
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
6
by: chuck amadi | last post by:
Hi , Im trying to parse a specific users mailbox (testwwws) and output the body of the messages to a file ,that file will then be loaded into a PostGresql DB at some point . I have read the...
0
by: Edvard Majakari | last post by:
I'm trying to create an improved front-end for training messages for a spam filter. I discovered an excellent Python module email - now it's so easy to support variety of mailbox formats. However,...
0
by: sarcastic | last post by:
Hi everyone, I am looking for a perl script that will be run from a linux (or unix) console, connects to a remote pop3 server and downloads the mails to local server (in a directory that i...
34
by: Reinhold Birkenfeld | last post by:
Hi, the arguments in the previous thread were convincing enough, so I made the Path class inherit from str/unicode again. It still can be found in CVS:...
27
by: Aurangzeb M. Agha | last post by:
I'm running Postgres 7.1.3, and just started having a problem where my dynamic site is going down (read-only DB, with no writes happening to the DB) regularly (every other day). I have no idea...
10
by: Eric | last post by:
Is there something to interface postgreSQL with QMail to store mails in pgsql instead of using mbox or maildir? Or maybe it's not a good idea to do that? I think there is some adavantages... ...
6
cassbiz
by: cassbiz | last post by:
Need help in setting up Dovecot for IMAP
3
by: Mark Shroyer | last post by:
I guess this sort of falls under the "shameless plug" category, but here it is: Recently I used a custom metaclass in a Python program I've been working on, and I ended up doing a sort of write-up...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.