# Settings
import platform
PNODE = platform.node()
CNAME = PNODE.replace('.local', '')

SMTP_SERVER = 'emailz.d27n.com'
SMTP_PORT = 587
SMTP_USERNAME = 'steve@d27n.com'
SMTP_PASSWORD = 'L2sC7JikwX'
SMTP_FROM = '' + CNAME + '@d27n.com'
SMTP_TO = 'steve@d27n.com'

SUBJECT = '[' + CNAME + '] Going to Sleep!'
TEXT_FILENAME = '/script/output/my_attachment.txt'
MESSAGE = """Goodnight!
"""

# Now construct the message
import smtplib, email
from email import encoders
import os

msg = email.MIMEMultipart.MIMEMultipart()
body = email.MIMEText.MIMEText(MESSAGE)
attachment = email.MIMEBase.MIMEBase('text', 'plain')
#attachment.set_payload(open(TEXT_FILENAME).read())
#attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(TEXT_FILENAME))
encoders.encode_base64(attachment)
msg.attach(body)
msg.attach(attachment)
msg.add_header('From', SMTP_FROM)
msg.add_header('To', SMTP_TO)
msg.add_header('Subject', SUBJECT)

# Now send the message
mailer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
# EDIT: mailer is already connected
# mailer.connect()
mailer.login(SMTP_USERNAME, SMTP_PASSWORD)
mailer.sendmail(SMTP_FROM, [SMTP_TO], msg.as_string())
mailer.close()
