
import sys
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# total arguments
n = len(sys.argv)

if (n != 4):
    print(f"\n   Usage: python {sys.argv[0]} emailAddress File subject\n")
    exit(0)

# =============================================================
# Change these settings
# =============================================================
username = "cheung@emory.edu"
password = "abc123"
mail_from = "cheung@emory.edu"


# =============================================================
# Get the parameters
# =============================================================
mail_to = sys.argv[1]
#mail_to = "cheung@emory.edu"
inpFile = sys.argv[2]
mail_subject = sys.argv[3]


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(inpFile, "r") as inp:
    mail_body = inp.read()

mimemsg = MIMEMultipart()
mimemsg['From']=mail_from
mimemsg['To']=mail_to
mimemsg['Subject']=mail_subject
mimemsg.attach(MIMEText(mail_body, 'plain'))
connection = smtplib.SMTP(host='smtp.office365.com', port=587)
connection.starttls()
connection.login(username,password)
connection.send_message(mimemsg)
connection.quit()
