Showing results for 
Search instead for 
Did you mean: 

saving mail body

Sudip Hore
Occasional Visitor

saving mail body


I want to automate a process of saving the body of a mail in a file whenever a new mail comes in and then delete the mail from a script.

Can you guys share some ideas?


Justo Exposito
Esteemed Contributor

Re: saving mail body

Hi Sudip,

You can edit the /etc/mail/aliases and put something like this:
some_aliases:"|cat - | mailx -s "hola" justo"
This send all the mails received by user some_aliases to other user by mail.

If you put:
some_aliases:"|cat - | scripttocut"
you can do the operation that you want. you only have to develop your scripttocut.
PD: you must run the newaliases command in order to made the changes efective.
Help is a Beatiful word
Steven Sim Kok Leong
Honored Contributor

Re: saving mail body


Have you tried using elm filters? With the ~/.forward, you can direct your inbound email as STDIN into your script.


The elm filter guide can be found at

The filter rules reside in $HOME/.elm/filter-rules eg.

# $HOME/.elm/filter-rules
# Filter rules for the Elm Filter program. Don't change without some
# serious thought. (remember - order counts)
# (for Dave Taylor)
# rule 1
if (from contains "!uucp") then delete
# rule 2
to "postmaster" ? save "/tmp/postmaster-mail.%d"
# rule 3
if (to "culture" and lines > 20) ? save "~/Mail/culture"
# rule 4
subject = "filter test" ? forward "hpldat!test"
# rule 5
if [ subject = "elm" ] savecopy "~/Mail/elm-incoming"
# rule 6
subject = "display-to-console" ? execute "cat - > /dev/console"

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
Email: Homepage:
Eugen Cocalea
Respected Contributor

Re: saving mail body


Get and install procmail. You can get it from:

Then, if you want to save every email body to a different file, add this to your .procmailrc file (in your home directory):

:0 b

To save all email bodies to a single file (appended), add this to the .procmailrc file:

:0 b

I tested this and it works.

To Live Is To Learn
Sudip Hore
Occasional Visitor

Re: saving mail body

Hi Eugen,

I have installed procmail and put this .procmailrc

MAILDIR=$HOME/Mail #you'd better make sure it exists
DEFAULT=$MAILDIR/mbox #completely optional
LOGFILE=$MAILDIR/from #recommended

:0 b

I have put the .forward file as follows

"|exec /opt/procmail/bin/procmail"

I am getting the following error message

$ /home/c727588/.forward: line 1: "|/opt/procmail/bin/procmail"... Addres
s is unsafe for mailing to programs
/home/c727588/.forward: line 1: "|exec /opt/procmail/bin/procmail"... Address c727588
is unsafe for mailing to programs
Can you help out?