import urllib.request, tempfile, smtplib, email, time from datetime import datetime, timedelta tmpdir = tempfile.TemporaryDirectory() beginning = str(1000*(int(time.time()))-(17*3600000))[:-3] end = str(1000*int(time.time()))[:-3] def get_boot_time(): with open('/proc/uptime', 'r') as f: uptime_seconds = int(float(f.readline().split()[0])) boot_time = datetime.now() - timedelta(seconds=uptime_seconds) + timedelta(hours=1) return boot_time def get_uptime(): with open('/proc/uptime', 'r') as f: uptime_hours = round(float(f.readline().split()[0])/3600, 2) return uptime_hours get_boot_time() # Send an HTML email with an embedded image and a plain text message for # email clients that don't want to display the HTML. urllib.request.urlretrieve("http://localhost/cacti/graph_image.php?local_graph_id=109&graph_start=" + beginning + "&graph_end=" + end + "&graph_width=700&graph_height=200", tmpdir.name + "/ping.png") urllib.request.urlretrieve("http://localhost/cacti/graph_image.php?local_graph_id=103&graph_start=" + beginning + "&graph_end=" + end + "&graph_width=695&graph_height=198", tmpdir.name + "/traffic.png") from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage # Define these once; use them twice! strFrom = 'pv-tannenstr@web.de' strTo = ["tobias.siegel@outlook.com", "wsiegel@web.de"] # Create the root message and fill in the from, to, and subject headers msgRoot = MIMEMultipart('related') msgRoot['Subject'] = datetime.today().strftime('%Y-%m-%d') + ' Darmstadt' msgRoot['From'] = strFrom msgRoot['To'] = ",".join(strTo) msgRoot.preamble = 'This is a multi-part message in MIME format.' # Encapsulate the plain and HTML versions of the message body in an # 'alternative' part, so message agents can decide which they want to display. msgAlternative = MIMEMultipart('alternative') msgRoot.attach(msgAlternative) msgText = MIMEText('This is the alternative plain text message.') msgAlternative.attach(msgText) # Get Boot Time and Convert it to String bt = get_boot_time() bt_str = bt.strftime("%d.%m.%Y, %H.%M") # We reference the image in the IMG SRC attribute by the ID we give it dasdasdbelow msgText = MIMEText('


Letzter Start: ' + bt_str + ' Uhr

', 'html') msgAlternative.attach(msgText) # This example assumes the image is in the current directory fp = open(tmpdir.name + '/traffic.png', 'rb') msgImage1 = MIMEImage(fp.read()) fp.close() fp = open(tmpdir.name + '/ping.png', 'rb') msgImage2 = MIMEImage(fp.read()) fp.close() # Define the image's ID as referenced above msgImage1.add_header('Content-ID', '') msgRoot.attach(msgImage1) msgImage2.add_header('Content-ID', '') msgRoot.attach(msgImage2) # Send the email (this example assumes SMTP authentication is required) import smtplib, ssl context = ssl.create_default_context() with smtplib.SMTP('smtp.web.de', 587) as server: server.ehlo() # Can be omitted server.starttls(context=context) server.ehlo() # Can be omitted server.login(strFrom, 'PV-Tannenstr1') server.sendmail(strFrom, strTo, msgRoot.as_string()) tmpdir.cleanup()