Files
schmeeve-toolz/send_status_mail3.py
2026-05-10 12:53:30 -07:00

82 lines
2.5 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.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
#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()
#creating the SMTP server object by giving SMPT server address and port number
smtp_server=smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
smtp_server.ehlo() #setting the ESMTP protocol
#smtp_server.starttls() #setting up to TLS connection
#smtp_server.ehlo() #calling the ehlo() again as encryption happens on calling startttls()
smtp_server.login(SMTP_USERNAME, SMTP_PASSWORD) #logging into out email id
msg_to_be_sent = MESSAGE # comment out next line to use embedded body
msg_to_be_sent = '<html><body style="font-size: 0.6em"><pre>' + data + '</body></html>'
message = MIMEMultipart("alternative")
message['From'] = SMTP_FROM
message['To'] = SMTP_TO
message['Subject'] = SUBJECT
#f = data.find("[st]")
#part1 = MIMEText(data[f+4:], "plain")
part1 = MIMEText(data, "plain")
part2 = MIMEText(msg_to_be_sent, "html")
message.attach(part1)
message.attach(part2)
#sending the mail by specifying the from and to address and the message
smtp_server.sendmail( SMTP_FROM, SMTP_TO, message.as_string())
smtp_server.quit() #terminating the server