Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

How to read email using mailx in shell script or perl

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old Unix and Linux 03-20-2008
jliharper jliharper is offline
Registered User
Join Date: Mar 2008
Last Activity: 10 July 2008, 3:46 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
How to read email using mailx in shell script or perl


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!!
Sponsored Links
Old Unix and Linux 03-20-2008
quine quine is offline
Registered User
Join Date: Mar 2008
Last Activity: 12 August 2009, 12:31 PM EDT
Location: Bay Area California
Posts: 68
Thanks: 0
Thanked 1 Time in 1 Post
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


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.

Sponsored Links
Old Unix and Linux 03-20-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
Join Date: Jan 2008
Last Activity: 11 September 2009, 3:44 PM EDT
Posts: 729
Thanks: 0
Thanked 0 Times in 0 Posts
You can also search CPAN for any mailx supporting modules.
Old Unix and Linux 03-20-2008
ghostdog74 ghostdog74 is offline
Registered User
Join Date: Sep 2006
Last Activity: 28 January 2015, 8:30 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts
you can parse the actual mailbox of that user itself , eg /var/mail/root, or using here document

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

Sponsored Links
Old Unix and Linux 03-23-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 11 Times in 9 Posts
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.
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
The scope of the shell/perl script is to read the input text file. Validate the expiry date of each casmo Shell Programming and Scripting 5 04-21-2011 11:06 AM
need shell or Perl script to read multiple input sreedhargouda Shell Programming and Scripting 8 08-13-2010 02:21 PM
Perl or Shell script to read a transaction log in real time bruno406 Shell Programming and Scripting 2 03-03-2009 10:50 AM
Perl script variable to read shell command bulkbiz Shell Programming and Scripting 4 10-10-2008 02:05 AM
Unable to send eMail from a UNIX-Host ( using mailx ) to a Outlook-email-addres(Win) Vetrivela UNIX for Advanced & Expert Users 2 02-15-2005 09:43 AM

All times are GMT -4. The time now is 08:06 AM.