Copy messages from a Thunderbird mbox file to a MySQL table using php

Written by: Maciej Grajcarek

Date July 1, 2008

If for some reason you would like to copy your messages from Thunderbird to a MySQL database, here you can find a simple PHP script which will do it for you! All you have to do is to set some variables in the php class and create a proper database table. Let’s start with the class.

var $file_name = 'some_mbox_file';
var $file_path = '/path/to/file/';

var $db_user = 'user';
var $db_password = 'pass';
var $db_host = 'localhost';
var $db_name = 'mbox';

$file_name is the name of the thunderbird mbox file and $file_path points to the folder it is located (deafult path in Windows looks sth like this: C:\\Documents and Settings\\%user%\\Application Data\\Thunderbird\\Profiles).
You should also define the database connection variables.

Next thing is to create a database table:


CREATE TABLE  `messages` (
`id` int(10) unsigned NOT NULL auto_increment,
`from` varchar(255) collate latin1_general_ci NOT NULL,
`to` varchar(255) collate latin1_general_ci NOT NULL,
`subject` varchar(255) collate latin1_general_ci NOT NULL,
`date` varchar(255) collate latin1_general_ci NOT NULL,
`message` text collate latin1_general_ci NOT NULL,
`received_ip` varchar(15) collate latin1_general_ci NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

As you can see, this script will read: from, to, subject, date, received_ip and message fields for each of messages. It will not copy attachments!

It supports also processing of big files. We have used it to parse files bigger than 3GB.

14 Responses to “Copy messages from a Thunderbird mbox file to a MySQL table using php”

  1. Diego said:

    Hi Maciej Grajcarek,

    I’m one of these people who for some kind of reason I want to convert all my Thunderbird emails to MySQL table. The problem is on the link, (it doesn’t work).
    Do you have the original file mboxreader.php?

    I will aprecciate your help because I tried Mbox class from phpclasses and it doesn’t work very well with the mbox Thunderbird files.

    Thanks in advance!!
    Diego Marcos

  2. daniel said:

    hi Marcos, we changed our svn server. sorry! you can access with u: anon, p: anon directly connecting with some svn client to svn.devayd.com

    regards, daniel

  3. Diego said:

    Oh! Thanks Daniel!!

    I will check it out, thanks for all!

  4. ale500 said:

    Hi Daniel,
    I tried to access at your svn server using TortoiseSVN but username and password seems does not exist…
    I’m using “anon” as username and “anon” as password..
    Please help me :)

    Thanks, alessio.

  5. DGT said:

    hey Daniel,
    after getting on your svn server, I cant seem to find the php script under any of the sub-directories. is it still there?
    thanks,
    DGT.

  6. DT said:

    hey Marcos,
    I used a svn client to get on your server but I cant find this script under any of the sub-directories. is it still there?
    thanks
    DT

  7. DT said:

    hey Daniel,
    I cant seem to find the script when I go on your svn server. what sub-directory is it under?
    sent this message before, but it did’nt post -sorry for the resend.
    thanks
    DT

  8. Jose said:

    Cant get this page
    http://websvn.devayd.com/wsvn/public/scripts/mboxreader.php
    I dont have SVN either.

  9. daniel said:

    hi,

    the exact command to get this file is:
    svn export svn://anon:anon@svn.devayd.com/cake/scripts/mboxreader.php

    you must have a svn client installed. there is no other way at the moment.

    regards
    daniel

  10. heru_kr said:

    file is not exist T_T

    anyone can send me that file “mboxreader.php” ?

    send to h312u_kurniawan at yahoo.com

    thanks

  11. cisses said:

    Hi Maciej,
    It’s seems to be a very nice tool for thunderbird mbox file!
    Could you send me please the php file (mboxreader.php) by email, please.
    It’s not possible to download it by http or svn …
    Thank you very much,
    Samba

  12. daniel said:

    hi, I will try to upload it to github soon. please watch http://github.com/danielzzz

  13. AG said:

    Found it here:
    https://github.com/danielzzz/mboxreader/blob/master/mboxreader.php

  14. Ser said:

    Thanks you very much for this piece of code it worked like a charm.

    I “imported” 15k bounces emails and now I can do a proper analysis.

    How can pay you a beer :D?

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>