How to read email using mailx in shell script or perl

Hello,

I am new to mailx and perl and I need help.

I need create a shell script to read the mails on the SUN server, then parse the subject line and message body of each email to extract particular data so that I can pass these data fields in as application parameters to invoke a java client application from the script. This logic need to be run every so many minutes to check for new email in the user's system mailbox.

I was thinking of using mailx in shell script. Is there a way I can parse the mail stream and pipe the extracted data to the java application inside mailx? Or do I have to copy the mail into a file, exit/quit mailx, then process the contents in the file later from the shell script? Or is there a better way using Perl?

Thank you!!

Perl supports opening pipes to processes as well as files.... so...

open(HNDL, "mailx -args |");

That last character is a PIPE. I don't remember all the args you need (I think mailx by default first gives you only headers, but there is an argument that tells it "just dump the whole mailbox")

So now you can do something like

while (<HNDL>)
{
# do all your parsing code line by line subjects AND bodies
}

close(HNDL);

Now this is WAY over simplified, because after you dump the mailbox you have to execute mailx again like system("mailx -args bla bla bla") or die;
or qx|mailx -args bla bla bla|, etc to DELETE the mail you've just read. But you have to be careful that you don't delete NEW mail that comes into the box while you are reading so count the headers and delete only those perhaps with something like mailx -d1-10 or something like that... Also, I don't know what kind of mail might show up in this box -- could it have attachments, etc.

There is a whole other approach you might find useful... I used to set up special accounts whose only purpose was to receive email of a certain kind, etc. Like we would tell our trading partners to send all X12 data to xyzaccount@myhost.com or what ever... xyzaccount then had a .forward file and the .forward REDIRECTED the email into my perl scripts...

so .forward would be something like "| myscript.pl". Thus everytime mail was received by xyzaccount it would be immediately piped to the script, one mail at a time. No need to open and read the mailbox....

Now one issue there is that sometimes for security reasons sysadmins won't let you FORWARD email to a PROCESS (as opposed to just another address). We had no problem with this since we expected only ONE kind of mail in that account and the first thing myscript.pl would do is make sure that it was the kind of mail we were expecting. If not, we just quit, issue a warning, etc.

quine@sonic.net

You can also search CPAN for any mailx supporting modules.

you can parse the actual mailbox of that user itself , eg /var/mail/root, or using here document
eg

mailx << EOF > outfile
 #mailx commands , eg type 1-5 
 # check the man page of such commands.
EOF 

Sounds pretty much like you would want to look at Procmail for the basic infrastructure. It can invoke a Perl script (or pretty much anything you like) on every incoming email message.