Sponsored Content
Top Forums Shell Programming and Scripting How to read email using mailx in shell script or perl Post 302177263 by quine on Thursday 20th of March 2008 01:27:26 PM
Old 03-20-2008
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
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

unable to do mailx from shell script

Hi From within a shell script my mailx doesnt seem to work...can somebody tell me what is wrong... #!/bin/ksh #Script to verify wheather all databases listed are up and running #Script works with Oracle8 and above databases #Script has to be run by ./scriptname DBA=xiamin@unix.com echo... (3 Replies)
Discussion started by: xiamin
3 Replies

2. UNIX for Advanced & Expert Users

Unable to send eMail from a UNIX-Host ( using mailx ) to a Outlook-email-addres(Win)

Hi A) I am able to send eMail using mailx from a UNIX ( solaris 8 ) host to my Outlook-email-ID : FName.Surname@Citigroup.com ( This is NOT my actual -eMail-ID). But in Outlook the "From :" eMail address is displayed as " usr1@unix-host1.unregistered.email.citicorp.com " .i.e the words... (2 Replies)
Discussion started by: Vetrivela
2 Replies

3. Shell Programming and Scripting

Perl script variable to read shell command

Solaris 10 Korn shell ksh, Hi there, I have figured out to get yesterday's date which is using the below command: TZ=GMT+24; date +%d-%b-%Y to get the format of 30-Sep-2008 and TZ=GMT+24; date +%Y%m%d to get the format of 20080930. I need this two format. In my perl script below I need... (4 Replies)
Discussion started by: bulkbiz
4 Replies

4. Shell Programming and Scripting

Perl or Shell script to read a transaction log in real time

Hello, I have a Apache webserver running on RedHat. Its primary function is a proxy server for users accessing the internet. I have a transaction log that logs every transactions of every users. For users trying to access certain sites/content the transactions goes into a 302 redirect loop and... (2 Replies)
Discussion started by: bruno406
2 Replies

5. Shell Programming and Scripting

need shell or Perl script to read multiple input

I need shell 0r Perl script to read multiple input and do something and come out example: echo “ enter the host names separated by space “ read servers foreach @servers { do do something done} Here host names like host1 host2 host3 . . . . . . . so on Please help me... (8 Replies)
Discussion started by: sreedhargouda
8 Replies

6. Shell Programming and Scripting

Script to read email from Esxchange OWA server

Hi all. I was asked to monitor our email process end to end, so that we can track that email is being delivered and the delay if any. The email system I am monitoring is a Microsoft exchange with the built in Microsoft Web front end. The monitoring systen is Nagios running on CentOS. I... (3 Replies)
Discussion started by: salatconed
3 Replies

7. Shell Programming and Scripting

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each certificate and send the mail to the user. The user takes action to add the new certificate to the storage file and user owns the responsibility to update the input text file with the new certificate... (5 Replies)
Discussion started by: casmo
5 Replies

8. Shell Programming and Scripting

Mailx, Table, Multiple Email script

Hello, I am able to write some simple scripts in linux but this time it seems a little bit hard for me. Objective: Script will read Database.txt file and will submit separate emails to multiple persons. Database.txt Elvis-Presley user_ssh1 ##20140403 firstperson@gmail.com Julia-Roberts... (3 Replies)
Discussion started by: baris35
3 Replies

9. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

10. UNIX for Beginners Questions & Answers

Shell Script to read hive table and send email

HI Team, I am working on reading hive table and send email in email body using shell script, can you please help on fixing the errors: I have 6 columns in my hive table and trying to send the email in the mail body. below script: #!/bin/bash hive -e 'SELECT count(*) from db.table' >... (4 Replies)
Discussion started by: Mi4304
4 Replies
HMINE(1)																  HMINE(1)

NAME
hmine - a mail message header analyzer. SYNOPSIS
hmine [-vDa] [FILE] hmine -V DESCRIPTION
hmine reads a mail message from FILE or STDIN and outputs a variety of information found in the message headers. The message is expected in Internet mail format (RFC 821,822,2821,2822 or variations thereof). The body is not inspected. EXIT STATUS
On success, hmine returns 1. In case of a problem, hmine returns zero. OPTIONS
-a Print mailboxes and groups found in various header fields, one per line, preceded by the field name. Actual email addresses are always enclosed in '<' and '>' for easy parsing, ie anything not within these delimiters is not part of an email address. Beware that not every line need contain an email address. -D Debug output. -V Print the program version number and exit. USAGE
An invocation looks like this: % hmine email.txt SOURCE
The source code for the latest version of this program is available at the following locations: http://www.lbreyer.com/gpl.html http://dbacl.sourceforge.net BUGS
At present, hmine parses messages but doesn't output anything useful. AUTHOR
Laird A. Breyer <laird@lbreyer.com> SEE ALSO
dbacl(1), mailcross(1), mailfoot(1), mailinspect(1), mailtoe(1), regex(7) Version 1.12 Bayesian Classification Tools HMINE(1)
All times are GMT -4. The time now is 12:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy