from.py 895 Bytes
Newer Older
1
#! /usr/bin/env python3
Guido van Rossum's avatar
Guido van Rossum committed
2 3 4 5 6

# Print From and Subject of messages in $MAIL.
# Extension to multiple mailboxes and other bells & whistles are left
# as exercises for the reader.

7
import sys, os
Guido van Rossum's avatar
Guido van Rossum committed
8 9 10

# Open mailbox file.  Exits with exception when this fails.

11
try:
12
    mailbox = os.environ['MAIL']
13
except (AttributeError, KeyError):
14 15
    sys.stderr.write('No environment variable $MAIL\n')
    sys.exit(2)
16 17

try:
18
    mail = open(mailbox)
19
except IOError:
20
    sys.exit('Cannot open mailbox file: ' + mailbox)
Guido van Rossum's avatar
Guido van Rossum committed
21 22

while 1:
23 24 25 26 27
    line = mail.readline()
    if not line:
        break # EOF
    if line.startswith('From '):
        # Start of message found
28
        print(line[:-1], end=' ')
29 30 31 32 33
        while 1:
            line = mail.readline()
            if not line or line == '\n':
                break
            if line.startswith('Subject: '):
34 35
                print(repr(line[9:-1]), end=' ')
        print()