1#! /usr/bin/env python
2
3# Print From and Subject of messages in $MAIL.
4# Extension to multiple mailboxes and other bells & whistles are left
5# as exercises for the reader.
6
7import sys, os
8
9# Open mailbox file.  Exits with exception when this fails.
10
11try:
12    mailbox = os.environ['MAIL']
13except (AttributeError, KeyError):
14    sys.stderr.write('No environment variable $MAIL\n')
15    sys.exit(2)
16
17try:
18    mail = open(mailbox)
19except IOError:
20    sys.exit('Cannot open mailbox file: ' + mailbox)
21
22while 1:
23    line = mail.readline()
24    if not line:
25        break # EOF
26    if line.startswith('From '):
27        # Start of message found
28        print line[:-1],
29        while 1:
30            line = mail.readline()
31            if not line or line == '\n':
32                break
33            if line.startswith('Subject: '):
34                print repr(line[9:-1]),
35        print
36