CoCalc -- Collaborative Calculation in the Cloud
Sharedwww / 129 / mail_list.pyOpen in CoCalc
#!/usr/bin/python
import os, string, sys

def get_list():
    f = open("list","r").read()
    list = []
    for line in string.split(f, "\n"):
        words = line.split()
        if len(words) < 2:
            continue
        list.append([words[0], words[len(words)-1]])
    return list

def mailall(filename):
    msg = open(filename,"r").read()
    if len(msg) < 100:
        print "Message is too short; I refuse to send your message!"
        sys.exit(1)
    if msg[:3].upper() == "SU:":
        i = string.find(msg,"\n")
        subject = msg[4:i]
        msg = msg[i+1:]
    else:
        print "Missing subject; I refuse to send your message!"
        sys.exit(1)        

    for person in get_list():
        name = person[0]
        m = string.replace(msg, "NAME", name)
        print "\n------------------------------------------"
        print "Sending the following message to %s:\n%s"%(person[1],m)
        mail = os.popen2('mail %s -s "%s"'%(person[1],subject))
        mail[0].write(m)
        mail[0].close()
        
if len(sys.argv) == 1:
    print "Usage: " + sys.argv[0] + " filename\n"
    sys.exit(1)

if not os.path.exists(sys.argv[1]):
    print sys.argv[0] + ": File name found"
    sys.exit(1)

mailall(sys.argv[1])