send_email.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import urllib.request, tempfile, smtplib, email, time
  2. from datetime import datetime, timedelta
  3. tmpdir = tempfile.TemporaryDirectory()
  4. beginning = str((1000*int(time.time()))-(17*3600000))
  5. end = str(1000*int(time.time()))
  6. def get_boot_time():
  7. with open('/proc/uptime', 'r') as f:
  8. uptime_seconds = int(float(f.readline().split()[0]))
  9. boot_time = datetime.now() - timedelta(seconds=uptime_seconds) + timedelta(hours=1)
  10. return boot_time
  11. def get_uptime():
  12. with open('/proc/uptime', 'r') as f:
  13. uptime_hours = round(float(f.readline().split()[0])/3600, 2)
  14. return uptime_hours
  15. get_boot_time()
  16. # Send an HTML email with an embedded image and a plain text message for
  17. # email clients that don't want to display the HTML.
  18. urllib.request.urlretrieve("http://localhost/cacti/graph_image.php?local_graph_id=109&graph_start=1657999980&graph_end=1658086115&graph_width=700&graph_height=200", tmpdir.name + "/ping.png")
  19. urllib.request.urlretrieve("http://localhost/cacti/graph_image.php?local_graph_id=103&graph_start=1658000546&graph_end=1658086946&graph_width=695&graph_height=198", tmpdir.name + "/traffic.png")
  20. from email.mime.multipart import MIMEMultipart
  21. from email.mime.text import MIMEText
  22. from email.mime.image import MIMEImage
  23. # Define these once; use them twice!
  24. strFrom = 'pv-tannenstr@web.de'
  25. strTo = ["tobias.siegel@outlook.com", "wsiegel@web.de"]
  26. # Create the root message and fill in the from, to, and subject headers
  27. msgRoot = MIMEMultipart('related')
  28. msgRoot['Subject'] = datetime.today().strftime('%Y-%m-%d') + ' Darmstadt'
  29. msgRoot['From'] = strFrom
  30. msgRoot['To'] = ",".join(strTo)
  31. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  32. # Encapsulate the plain and HTML versions of the message body in an
  33. # 'alternative' part, so message agents can decide which they want to display.
  34. msgAlternative = MIMEMultipart('alternative')
  35. msgRoot.attach(msgAlternative)
  36. msgText = MIMEText('This is the alternative plain text message.')
  37. msgAlternative.attach(msgText)
  38. # Get Boot Time and Convert it to String
  39. bt = get_boot_time()
  40. bt_str = bt.strftime("%d.%m.%Y, %H.%M")
  41. # We reference the image in the IMG SRC attribute by the ID we give it dasdasdbelow
  42. msgText = MIMEText('<img src="cid:image1"><br><img src="cid:image2"><br><br><p>Letzter Start: ' + bt_str + ' Uhr</p>', 'html')
  43. msgAlternative.attach(msgText)
  44. # This example assumes the image is in the current directory
  45. fp = open(tmpdir.name + '/traffic.png', 'rb')
  46. msgImage1 = MIMEImage(fp.read())
  47. fp.close()
  48. fp = open(tmpdir.name + '/ping.png', 'rb')
  49. msgImage2 = MIMEImage(fp.read())
  50. fp.close()
  51. # Define the image's ID as referenced above
  52. msgImage1.add_header('Content-ID', '<image1>')
  53. msgRoot.attach(msgImage1)
  54. msgImage2.add_header('Content-ID', '<image2>')
  55. msgRoot.attach(msgImage2)
  56. # Send the email (this example assumes SMTP authentication is required)
  57. import smtplib, ssl
  58. context = ssl.create_default_context()
  59. with smtplib.SMTP('smtp.web.de', 587) as server:
  60. server.ehlo() # Can be omitted
  61. server.starttls(context=context)
  62. server.ehlo() # Can be omitted
  63. server.login(strFrom, 'PV-Tannenstr')
  64. server.sendmail(strFrom, strTo, msgRoot.as_string())
  65. tmpdir.cleanup()