HOWTO: remail contents of an mbox

If you are like me and you enjoy running somewhat of a lean and clean machine, you are going to love this HOWTO! It is very simple to remail the contents of an mbox using the procmail port. This will be particularly useful if one failed to, oh I don't know, forward root's email to another mailbox perhaps.

:r

Some extra mailing info will be in the respooled headers, but the main benefit is that the *original* date/time stamp is preserved. Assumptions here are that Sendmail is the MTA and it is at a default configuration having not been asked to do anything else post-install.

To get started.

1. Update your ports tree: portsnap fetch update

2. Change directory: cd /user/ports/mail/procmail/

3. Build the port: make clean install



After the port is built you will need to edit /etc/mail/freebsd.mc to make procmail the "Local Delivery Agent".

1. Change directory: cd /etc/mail/

2. Edit the .mc file: edit freebsd.mc

3. Line 55 will most likely be the last line of the "FEATURE" entries. If so, on line 56 add:
Code:
FEATURE(local_procmail)

4. The "MAILER" options should be at the end of the file. If so, add this line as the last line of the file:
Code:
MAILER(procmail)

5. To save the changes, press CTRL+C to bring up "command:" and then type exit to save the file and exit the editor.

6. Rebuild the .cf file from the edited .mc file *and* restart sendmail: make all install restart



Time to test your work.

1. In your non-root user home directory type: edit .procmailrc

2. Add this line:
Code:
LOGFILE=$HOME/procmail.log

The thought here is that the first time you get an e-mail delivered to your mail spool, $HOME/procmail.log will be created and information about the e-mail will be recorded. Consecutive mail deliveries will append to $HOME/procmail.log. If everything is happening as it should you're in good shape.

3. To save the changes, press CTRL+C to bring up "command:" and then type exit to save the file and exit the editor.

4. Send a test email: mail {your non-root user}

5. Type a reasonable (or unreasonable depending on your mood) "Subject:" and press "Enter". Type something for the body of the email and press "Enter" once again. To send the email press "CTRL+D". The email will be sent if you see "EOT" displayed.

If the everything is working correctly you should now see a new file in the root of the home directory of your user named procmail.log. Here are the contents of the procmail.log file from the test I did:
Code:
From john.blue@---.com  Tue Oct 15 01:23:38 2013
 Subject: test
  Folder: /var/mail/john.blue                769



Prepping for the respooling of the mbox.

1. In the root of the home directory of the user that you want to respool mail for type: edit .procmailrc

2. Add these lines:
Code:
:0
*
! new.email@address.com

3. To save the changes, press CTRL+C to bring up "command:" and then type exit to save the file and exit the editor.

4. Finally, send a test email to the user to be respooled and see if it is routed correctly to the "new.email@address.com" email address.



Time to pull the trigger -- live firing!

1. Safety check troopers. Stop and think. Remember, this is the real deal here, if you have messed it up you will be spewing a lot of email someplace you might would rather not.

Up till this point has everything look okay? If so, lock-n-load!

cat /var/mail/capt.awesomeuser | formail -s procmail

Depending on the size of the mbox will determine how long it takes to parse it. I respooled almost 600 emails in under 30 seconds.



Clean up action items:

1. Is this a one time shot? If so, you can remove procmail.
2. If procmail is removed, delete any .procmailrc files that you may have created. Otherwise, you might want to re-edit .procmailrc of the user that the email was respooled from and remove the three lines to keep future email from being forwarded.
3. Once you have determined all of the emails have been delivered, do you need to keep the content of the old mail file?
4. If you have elected to remove procmail, you will need to back out the changes that you made to /etc/mail/freebsd.mc and re-run make all install restart.
 
Back
Top