Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

CentOS 5.x | SendMail

Hi guys,

Is there an easy way to retrieve individual messages from /var/spool/mail/? I have an idea for a really simple script and would like to check each individual message for something.

The scenario would be:

1) new email arrives in inbox. 2) Script checks for certain string. 3) If string is found, script retrieves various header info and emails info to address.

I realize that a milter would probably be the way to go but I was hoping to find a way to do this with a shell script as I want to practice that. The problem is that I can't seem to find a way to parse or otherwise isolate the individual messages. When I examine /var/spool/mail/, I see a big file with multiple messages and no unique delimiter between messages.

share|improve this question
up vote 2 down vote accepted

If you ignore simple shell scripts almost every single programming language has a library to parse a mbox formated mailbox.

Of course if you look around you can also find lots of POP3 and IMAP client libraries as well.

You should also be able to use something like procmail or maildrop to filter your messages.

share|improve this answer
Cool. I was planning to learn python anyway... I guess I might as well do that now. Thanks! – Mike B Aug 11 '10 at 19:08

Classically you would do this kind of things when the mail is received, using an MDA such as procmail. If you already have a mailbox, procmail comes with formail(1) which lets you run a program on each mail in a mailbox and has facilities to filter and rewrite headers.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.