
import sys
import os

# total arguments
n = len(sys.argv)

if (n != 4):
    print(f"\n   Usage: python {sys.argv[0]} emailFile inputFile subject\n")
    print("\nThis program sends out inputFile to all receivers in emailFile\n")
    exit(0)

emailFile = sys.argv[1]
inpFile = sys.argv[2]
subject = sys.argv[3]

if (not os.path.isfile(emailFile)):
    print(f"\n   Error: email file '{emailFile}' does NOT exists\n")
    exit(0)

if (not os.path.isfile(inpFile)):
    print(f"\n   Error: inputfile '{inpFile}' does NOT exists\n")
    exit(0)

#mail_body = "This message was sent with Python."

with open(emailFile, "r") as inp:
    emails = inp.readlines()
# print(emails)

out = []
out = (x.strip() for x in emails)
out = list(out)
# print(out)

for e in out:
    print(f"Sending '{inpFile}' to '{e}'")

    cmd = "python sendmail.py "    
    cmd = cmd + str(e) + " " + str(inpFile) + " '" + str(subject) + "'"

#   print(f"{cmd}")         # DEBUG: show command
#   os.system(cmd)
