53 lines
1.3 KiB
Python
53 lines
1.3 KiB
Python
# Settings
|
|
import platform, os
|
|
|
|
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'
|
|
|
|
SUBJ = os.getenv('ESUBJ')
|
|
if not SUBJ:
|
|
SUBJ = "Status Notification"
|
|
SUBJECT = '[' + CNAME + '] ' + SUBJ;
|
|
TEXT_FILENAME = '/script/output/my_attachment.txt'
|
|
MESSAGE = """Status
|
|
"""
|
|
|
|
with open ('/tmp/statusmail.txt', 'r') as file:
|
|
data = file.read()
|
|
|
|
#from pathlib import Path
|
|
#txt = Path('data.txt').read_text()
|
|
|
|
|
|
# Now construct the message
|
|
import smtplib, email
|
|
from email import encoders
|
|
import os
|
|
|
|
msg = email.MIMEMultipart.MIMEMultipart()
|
|
body = email.MIMEText.MIMEText(data)
|
|
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()
|