import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

username = "cheung@emory.edu"
password = "abc123"
mail_from = "cheung@emory.edu"
#mail_to = "cheung@emory.edu"
mail_to = "dumboride@yahoo.com"
mail_subject = "Python program to attach a file"
mail_body = "This message was sent with Python - with attachment."


#mail_attachment="https://d1ny9casiyy5u5.cloudfront.net/tmp/test.txt"
mail_attachment="attach.py"
mail_attachment_name="attach.py"

mimemsg = MIMEMultipart()
mimemsg['From']=mail_from
mimemsg['To']=mail_to
mimemsg['Subject']=mail_subject
mimemsg.attach(MIMEText(mail_body, 'plain'))

with open(mail_attachment, "rb") as attachment:
    mimefile = MIMEBase('application', 'octet-stream')
    mimefile.set_payload((attachment).read())
    encoders.encode_base64(mimefile)
    mimefile.add_header('Content-Disposition', 
		"attachment; filename= %s" % mail_attachment_name)
    mimemsg.attach(mimefile)
    connection = smtplib.SMTP(host='smtp.office365.com', port=587)
    connection.starttls()
    connection.login(username,password)
    connection.send_message(mimemsg)
    connection.quit()


