Operating System - HP-UX
1846707 Members
3315 Online
110256 Solutions
New Discussion

Re: doubt regarding mail message

 
Amadeus_1
Regular Advisor

doubt regarding mail message

Hi,

I am new to UNIX and actually I am not sure if I should be asking this question on this forum. But I ve been using this forum for HPOV and it is a super forum.

Well the doubt I ve got is ...

I need to write a script to monitor if an user gets a new mail.

When ever an User gets a new mail, I need to generate an alert in HPOV. This part I can do, but am not sure on how to check for new mails.

I checked for the command mail -e, but this option tells me if I have a mail in the mailbox or not.
I am looking for only new mails.

8 REPLIES 8
Sandman!
Honored Contributor

Re: doubt regarding mail message

Look inside the user's incoming mail file under /var/mail/.
Amadeus_1
Regular Advisor

Re: doubt regarding mail message

Thanks Sandman,

But I need to run a script every fifteen minutes that would require to do this.

n if I check for the file under /var/mail/ i would need to check for the time stamp.

I thought there would be an easier way to do this.

A. Clay Stephenson
Acclaimed Contributor

Re: doubt regarding mail message

I would actually do this in Perl because all the pieces you need are there.

Initially, create a file which lists
/var/mail/usernamelastmodificationtime
for each users mailfile.

Something like thisi, myfile1.pl:
---------------------------------------
#!/usr/bin/perl -w

my $i = 0;
while ($i <= $#ARGV)
{
print $ARGV[$i],"\t",(stat($ARGV[$i])) [9],"\n";
++$i;
}
-------------------------------------
Invoke it like this:
perl myfile1.pl /var/mail/* > myreferencefile

Now your script will look for any files with newer modification times or files that are not in your reference files. After doing whatever operation you wish, you update the reference file with the new timestamps and any detected new files.

I'm not saying you must use Perl but it will be easy in Perl and a bit tricky in the shell because of the timestamp comparisons.
If it ain't broke, I can fix that.
Amadeus_1
Regular Advisor

Re: doubt regarding mail message

thanks Clay,

But I was wondering,

When we logon, there appears a message that says "you have a new mail" which runs because of the profile.
There is a command that says

/bin/mail -E
case $? in
0)
echo "You have new mail."
;;
2)
echo "You have mail."
;;
esac


but I am unsure if this will satisfy my case,
because instead of echo "You have new mail", i can write the command to generate that alert.

Is this the same that I am looking at ??
A. Clay Stephenson
Acclaimed Contributor

Re: doubt regarding mail message

You can run
mail -e
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "New mail"
fi

but this is specific to a user but I interpreted your question to mean detect when any user gets new mail. Since it's related to OV, I would much rather write one routine that handles all of my OV users and requires only 1 cron entry --- but that's just me.
If it ain't broke, I can fix that.
Amadeus_1
Regular Advisor

Re: doubt regarding mail message

Hi Clay,

I checked this option but echo $? returns a value of 0 even when the mail has been read.

I want to check only for new mails.

$? does not return a value of 2 at all.

A. Clay Stephenson
Acclaimed Contributor

Re: doubt regarding mail message

Well, my version of mail does mot have a -E option, mail -e does work as advertised and indicates that mail is present but not new mail. I've already shown you the way to do it, so get busy.
If it ain't broke, I can fix that.
Amadeus_1
Regular Advisor

Re: doubt regarding mail message

Okie .. :)