How to read email using mailx in shell script or perl | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to read email using mailx in shell script or perl

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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

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!!
Sponsored Links
    #2  
Old 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
}

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
Sponsored Links
    #3  
Old 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.
    #4  
Old 03-21-2008
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 9 November 2014, 12:24 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
eg

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

Sponsored Links
    #5  
Old 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 10 Times in 8 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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 12:06 PM
need shell or Perl script to read multiple input sreedhargouda Shell Programming and Scripting 8 08-13-2010 03:21 PM
Perl or Shell script to read a transaction log in real time bruno406 Shell Programming and Scripting 2 03-03-2009 11:50 AM
Perl script variable to read shell command bulkbiz Shell Programming and Scripting 4 10-10-2008 03: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 10:43 AM



All times are GMT -4. The time now is 12:15 PM.