64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
# 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.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 = data
|
|
|
|
|
|
message = MIMEMultipart()
|
|
message['From'] = SMTP_FROM
|
|
message['To'] = SMTP_TO
|
|
message['Subject'] = SUBJECT
|
|
message.attach(MIMEText(msg_to_be_sent, 'plain'))
|
|
|
|
#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 |