READING mail from /var/mail/user


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting READING mail from /var/mail/user
# 1  
Old 05-20-2006
READING mail from /var/mail/user

Hello guys,

I'm in desperate need. I need to write a script that behaves like a "mail" shell program.

I've a base of the program -menu and so on, but what I cannot do is how to read messages from /var/mail/user. I would like to separate them as "mail" program does, but I;m just a newbie and cannot make it. I've tried it with grep -> so i get the line position of the mail header and then splitting up the messages, but I run to problems with saving lines and so on.

I will appreciate any help guys.
Thanks,
Peto
# 2  
Old 05-20-2006
Any ideas how to get just one message from /var/mail/user?
# 3  
Old 05-20-2006
Lemme ask you a question.

What do you think separates one message from the other?
# 4  
Old 05-20-2006
Well, as far I know it is separated by Headers
From 'name' 'date' ...
TEXT

From ...

But, there is some TEXT just behind the header that isn't printed out by 'mail', cuz its info about the delivery. But though I know this, I cannot make it. I tried to get positions of headers with grep, but I couldn't get it to the end.
# 5  
Old 05-20-2006
ok, that's good.
What's the CONSISTENT first line of EVERY mail message?

Pls post a couple of samples here.
# 6  
Old 05-20-2006
THIS IS ONE MESSAGE(TEXT in RED is written by MAIL program as Message content - I'd like the same):
Code:
From newbie@gmail.com Thu May 18 10:17:58 2006
Return-Path: <newbie@gmail.com>
Received: from relay.cv.cm (relay.cv.cm[147.32.80.7])
	by cv.cm (8.12.10+Sun/8.12.10) with ESMTP id k4I8Hw9W010121
	for <newbie@cv.cm>; Thu, 18 May 2006 10:17:58 +0200 (MEST)
Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.181])
	by relay.cv.cm (8.13.4/8.13.4) with ESMTP id k4I8Hanf067227
	for <cv.cm>; Thu, 18 May 2006 10:17:55 +0200 (CEST)
	(envelope-from newbie@gmail.com)
Received: by py-out-1112.google.com with SMTP id t32so538943pyc
        for <newbie@cv.cm>; Thu, 18 May 2006 01:17:55 -0700 (PDT)
DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws;
        s=beta; d=gmail.com;
        h=received:message-id:date:from:to:subject:mime-version:content-type;
        b=nX4ViyhbdhgEfk92jDdHtfB1+4gGMImzF5znA9m/g3tJsqhAzg9LPI/b3NllfQB51SpfbP8NyG9oh8W7LSB8d9xBINz2rdPPX4HIAGBqSYvbOKod4eNU=
Received: by 10.35.40.10 with SMTP id s10mr241034pyj;
        Thu, 18 May 2006 01:17:55 -0700 (PDT)
Received: by 10.35.9.4 with HTTP; Thu, 18 May 2006 01:17:55 -0700 (PDT)
Message-ID: <7a04f4e906051098098516abbab9@mail.gmail.com>
Date: Thu, 18 May 2006 01:17:55 -0700
From: "ppppp" <newbie@gmail.com>
To: newbie@cv.cm
Subject: Noooo
MIME-Version: 1.0
Content-Type: multipart/alternative; 
	boundary="----=_Part_107903_11845770.1147940275262"
X-FELK-MailScanner-Information: Please contact the ISP for more information
X-FELK-MailScanner: Found to be clean
X-FELK-MailScanner-SpamCheck: not spam, SpamAssassin (score=-0.388,
	required 5, BAYES_00 -2.60, HTML_MESSAGE 0.00,
	HTML_SHORT_LENGTH 1.57, SARE_MSGID_LONG40 0.64, SPF_PASS -0.00)
X-FELK-MailScanner-From: newbie@gmail.com

Content-Length: 495

------=_Part_107903_11845770.1147940275262
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Nemam co napssaaat.


sssss

------=_Part_107903_11845770.1147940275262
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

<div>Nemam co napssaaat.</div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>sssss</div>

------=_Part_107903_11845770.1147940275262--

# 7  
Old 05-20-2006
how about something like this for starters:

nawk -f peto.awk path2VarMailUser

peto.awk:
Code:
BEGIN {
  # make 'records' to be separated by blank/empty lines
  RS=FS=""

  # create a pattern where something starts either one of listed strings
  headers="(^From )|(^Message-ID:)|(^Date:)|(^From:)|(^To:)|(^Subject:)"
}

# If a record starts with 'From ' ...
/^From / {

   # print the record number
   printf("\n\n=== THIS is MAIL MESSAGE #%d ===\n\n", ++msg)

   # iterate through the fields of a given 'record' - every field is a LINE, 
   # because records are 'blank line' separated sequesnce of lines
   for(i=1; i<=NF; i++) {

     # if a record field (a line) matches a pattern 'headers' - print that field
     if ( $i ~ headers) print $i
   }
}

# print any OTHER 'record' - this is a shorthand for 'print $0'
1


Last edited by vgersh99; 05-21-2006 at 06:15 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Client was not authenticated to send anonymous mail during MAIL FROM (in reply to MAIL FROM comm

I am having trouble getting mail to work on a red hat server. At first I was getting this message. Diagnostic-Code: X-Postfix; delivery temporarily suspended: connect to :25: Connection refused Then added the port to my firewall. Then I temporarily turned off selinux. I then copied this file... (1 Reply)
Discussion started by: cokedude
1 Replies

2. AIX

/var/spool/mail/ issues

Hi My box is running with AIX 6100-06 and Im the root user of this box My /var gets filled up often to 100% When I investigate I find that it is the below file which increases rapidly /var/spool/mail/pdgadmin I dont know why this file is growing up. Can any one assist me on this.... (2 Replies)
Discussion started by: samsungsamsung
2 Replies

3. UNIX for Advanced & Expert Users

need to configure mail setting to send mail to outlook mail server

i have sun machines having solaris 9 & 10 OS . Now i need to send mail from the machines to my outlook account . I have the ip adress of OUTLOOK mail server. Now what are the setting i need to do in solaris machines so that i can use mailx or sendmail. actually i am trying to automate the high... (2 Replies)
Discussion started by: amitranjansahu
2 Replies

4. Shell Programming and Scripting

importance of /var/mail

Hi When an entry will be made to the file /var/mail/<user-id> . I have 100 scripts under a specific user id(dgircc) in cron .SO inrder to check the whether the script has sucessfully run or not and if not to generate an email if i mention the code like #!/bin/ksh -p 2 fsize=`ls -lrt... (0 Replies)
Discussion started by: mskalyani
0 Replies

5. UNIX for Dummies Questions & Answers

Parse /var/mail

Hi all... We have a box that receives a lot of incoming emails. I have a .procmailrc file that in turn invokes a python script, which process each of these incoming email. All is nice and good. However, before the .procmailrc and python script were in place, we still were receiving emails and... (0 Replies)
Discussion started by: khader69
0 Replies

6. UNIX for Advanced & Expert Users

Mail going to /var/spool/mqueue instead of being sent

Hello, I have a bunch of cron jobs in the crontab. For some reason mail from the cron jobs started going to /var/spool/mqueue instead of being sent. Does anyone know why mail from cron jobs would go to the queue instead of being sent? (9 Replies)
Discussion started by: xadamz23
9 Replies

7. UNIX for Dummies Questions & Answers

/var/spool/mail

Hi, How can i get my mail on either /var/spool/mail or /var/mail? I use mail and sendmail command to send mail. But everytime I send mail it comes to my outlook inbox and when I check with mail command I get the message "No mail for siba". (Note siba is my user Id.) (2 Replies)
Discussion started by: siba.s.nayak
2 Replies

8. UNIX for Dummies Questions & Answers

How do I read mail in /var/spool/mail?

How can I read mail sent to /var/spool/mail? I do not have pine installed so forget about that...is there some generic utility I can use? (3 Replies)
Discussion started by: mojoman
3 Replies

9. Solaris

how to forward mail in /var/mail/username to external mail

Dear All, Now I use solaris 10 and I try to forward mail from /var/mail/username to their external mail so what should I do? thank u in advance (2 Replies)
Discussion started by: unitipon
2 Replies

10. UNIX for Dummies Questions & Answers

Can I zero var/spool/mail/mail

The mail file in the directory var/spool/mail is very large. Can I zero this (>) without losing any unopened mails there may be? There are about 10 mail accounts. Using RedHat Linux. Thanks in advance. (0 Replies)
Discussion started by: dennisheazle
0 Replies
Login or Register to Ask a Question