send_email.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. from email.mime.multipart import MIMEMultipart
  19. from email.mime.text import MIMEText
  20. from email.mime.image import MIMEImage
  21. # Define these once; use them twice!
  22. strFrom = 'pv-tannenstr@web.de'
  23. strTo = ["tobias.siegel@outlook.com", "wsiegel@web.de"]
  24. # Create the root message and fill in the from, to, and subject headers
  25. msgRoot = MIMEMultipart('related')
  26. msgRoot['Subject'] = datetime.today().strftime('%Y-%m-%d') + ' Darmstadt'
  27. msgRoot['From'] = strFrom
  28. msgRoot['To'] = ",".join(strTo)
  29. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  30. # Encapsulate the plain and HTML versions of the message body in an
  31. # 'alternative' part, so message agents can decide which they want to display.
  32. msgAlternative = MIMEMultipart('alternative')
  33. msgRoot.attach(msgAlternative)
  34. msgText = MIMEText('This is the alternative plain text message.')
  35. msgAlternative.attach(msgText)
  36. # Get Boot Time and Convert it to String
  37. bt = get_boot_time()
  38. bt_str = bt.strftime("%d.%m.%Y, %H.%M")
  39. # We reference the image in the IMG SRC attribute by the ID we give it dasdasdbelow
  40. msgText = MIMEText('<br><p>Letzter Start: ' + bt_str + ' Uhr</p>', 'html')
  41. msgAlternative.attach(msgText)
  42. # Send the email (this example assumes SMTP authentication is required)
  43. import smtplib, ssl
  44. context = ssl.create_default_context()
  45. with smtplib.SMTP('smtp.web.de', 587) as server:
  46. server.ehlo() # Can be omitted
  47. server.starttls(context=context)
  48. server.ehlo() # Can be omitted
  49. server.login(strFrom, 'PV-Tannenstr')
  50. server.sendmail(strFrom, strTo, msgRoot.as_string())
  51. tmpdir.cleanup()