The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Email sending through a shell script memonks UNIX for Dummies Questions & Answers 3 11-11-2007 08:21 PM
Mailx in shell script (KSH) rockysfr Shell Programming and Scripting 2 07-01-2007 03:08 AM
How to use mail,mailx command in Shell Script ? sollin Shell Programming and Scripting 16 04-28-2006 03:50 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 06:43 AM
unable to do mailx from shell script xiamin Shell Programming and Scripting 3 12-23-2002 11:10 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-20-2008
Registered User
 

Join Date: Mar 2008
Posts: 2
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!!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-20-2008
Registered User
 

Join Date: Mar 2008
Location: Bay Area California
Posts: 61
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
Reply With Quote
  #3 (permalink)  
Old 03-20-2008
Registered User
 

Join Date: Jan 2008
Posts: 294
You can also search CPAN for any mailx supporting modules.
Reply With Quote
  #4 (permalink)  
Old 03-20-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,423
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
Reply With Quote
  #5 (permalink)  
Old 03-23-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,707
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.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:53 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0