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

Through some missconfiguration i've a lot of emails in /var/mail/root and /var/mail/www-data

How can i "loop" over these files and send each mail another time?

Thanks in advance!

share|improve this question

migrated from Dec 2 '09 at 2:40

This question came from our site for professional and enthusiast programmers.

formail, part of procmail (and probably already available on your system) can take a mailbox, split it up into messages, and then run a command on each one. From the man page:

   -s   The input will be split up into separate mail messages, and  piped
        into  a  program  one  by  one (a new program is started for every
        part).  -s has to be the last option specified, the first argument
        following  it  is  expected to be the name of a program, any other
        arguments will be passed along to it.  If you  omit  the  program,
        then  formail  will  simply  concatenate the split mails on stdout
        again.  See FILENO.

So you can do what I think you want like this:

  formail -s /usr/sbin/sendmail -oi -t < /var/mail/root
share|improve this answer
But also what kriss said re: addressing; this solution assumes that you've fixed your local aliases or something to send the messages somewhere other than back into /var/mail/root. You could, for example, do "formail -s /usr/sbin/sendmail -oi you@somewhere.else" to redirect all the messages. – larsks Dec 2 '09 at 3:14
assuming that www-data and root are now properly configured, something like "for foo in ls /var/mail; do formail -s /usr/sbin/sendmail -oi $foo < ./$foo ; done" should loop each file and locally deliver.. – Tom H Jan 10 '12 at 10:51
mutt -f /var/mail/www-data

Then within mutt...


T puts it into tagging mode, and .* tags all messages. The semi-colon applies the next command to all tagged messages, and finally b "bounces" the messages to an address it will prompt for.

From memory, I think either




Will then empty the mailbox.

share|improve this answer

The /var/spool/mail/xxx unix mailbox files are simple text files where mail messages are kept in sequence. Each mail message begin by a "From" line (taht is not part of the message) with sender and date of reception, then you get mail headers, then mail body.

Basically you can just loop on the file detecting such From lines and extract every messages then send them using tools like python smtp module or perl Net::SMTP module from cpan.

If target is another local mailbox you can just append the messages (including the From line) to it. You could even append the whole root mailbox to another mailbox if you don't care also getting of messages really targeted to root.

I should have some scripts that do that and will post exemples if you want.

The problem may be to get the real target of the message because if they ended in root mailbox they were probably targeted to root from the begining ?

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.