Extracting mails from mailbox

I wanted a script to automatically watch for new emails, get the sender, subject and attachments from them, move the attachments to a directory and insert sender/subject info into a MySQL database.

I needed a solution for extracing mail files from big UNIX-style mailbox files, then parse them and extract attachments separately.


Here’s the script that does the job.

The script works by periodically checking /var/mail/chat for size change, then getting all mails from that mailbox file to a temp directory (/tmp/chatmailparser/). Then sender & subject get extracted, decoded with an external PHP script:

1
2
3
4
5
#!/usr/bin/php5
<?php
    mb_internal_encoding("UTF-8");
    echo mb_decode_mimeheader($argv[1]);
?>

Attachments are saved to the temp folder also using this Perl script:

1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/perl

use MIME::Parser;

my $output_dir = $ARGV[0] || die('no output dir');
my $inputfile = $ARGV[1] || die('no input file');

my $parser = MIME::Parser->new();
$parser->output_dir($output_dir);
my $entity = $parser->parse_open($inputfile);

exit;

After moving them to their destination (modifying filenames if files already exist), the script puts chat message data into a MySQL database.

I’m using this init script for starting the chatmailparser script.

Name (required)
E-mail (required - never shown publicly)
Webpage URL
Comment:
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> in your comment.

Trackback responses to this post

About me

Nonoo
I'm Nonoo. This is my blog about music, sounds, filmmaking, amateur radio, computers, programming, electronics and other things I'm obsessed with. ... »

Twitter

Listening now

My favorite artists