From dedd76b950c4af3f79bcec5ec4b5d8e61e6d42c2 Mon Sep 17 00:00:00 2001 From: Rabjerg Date: Mon, 22 Mar 2021 16:23:32 +0100 Subject: [PATCH] Improvements to mail template. --- mail_template.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/mail_template.py b/mail_template.py index 443fe65..d97ad96 100755 --- a/mail_template.py +++ b/mail_template.py @@ -9,15 +9,19 @@ import os from platform import node from email.utils import make_msgid -## Setup variables +## Setup basic variables HOSTNAME = node() recivers = ["example@example.com", "example2@example.com"] subject = f"Backup on \"{HOSTNAME}\", for SERVICE complete." + +## here is a good place to add any aditional data you want to report, like system utilisation. message = f"Result of backup on server {HOSTNAME}\n" +## LASTLOG is the path defined in backup.env, this is read and put in the message. + if "LASTLOG" not in os.environ: print("No logfile found, please set environment variable LASTLOG to the borg logfile") exit(5) @@ -29,7 +33,6 @@ with open(logfile, "r") as log: message += line ## Load mail config from secrets file. - config = configparser.ConfigParser() config.read('/home/adam/scripts/secrets.env') @@ -46,7 +49,9 @@ def send_email(reciever, sender, subject, message): server.login(sender, server_pass) ## Yes we build the header entry for entry, this is done for readability. - + ## The header has all this extra infomation to make the mail relays happy and not trigger any antispam. + ## We want to be nice. + ## Don't mess with anything (like remove tailing newline) but the sender/reciever/subject unless you really know what you are doing. header = "" header += f"From: {sender}\n" @@ -60,11 +65,15 @@ def send_email(reciever, sender, subject, message): header += "Content-Disposition: inline\n" header += "Content-Transfer-Encoding: 8bit\n" - ## And now we make the payload. - - msg = f"{header}\n{message}" # The /n separates the message from the headers + ## And now we make the payload and send our email. + msg = f"{header}\n{message}" server.sendmail(sender, receiver, msg.encode("utf-8")) + + ## Close connection and return any errors. + return server.quit() + +## Here we trigger the sending of mail(s) for receiver in recivers: - send_email(receiver , sender, subject, message) + send_email(receiver, sender, subject, message)