send_email.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import urllib.request, tempfile, smtplib, email, time
  2. from datetime import datetime, timedelta
  3. from influxdb import InfluxDBClient
  4. tmpdir = tempfile.TemporaryDirectory()
  5. beginning = str((1000*int(time.time()))-(17*3600000))
  6. end = str(1000*int(time.time()))
  7. def get_boot_time():
  8. with open('/proc/uptime', 'r') as f:
  9. uptime_seconds = int(float(f.readline().split()[0]))
  10. boot_time = datetime.now() - timedelta(seconds=uptime_seconds) + timedelta(hours=1)
  11. return boot_time
  12. def get_uptime():
  13. with open('/proc/uptime', 'r') as f:
  14. uptime_hours = round(float(f.readline().split()[0])/3600, 2)
  15. return uptime_hours
  16. get_boot_time()
  17. client = InfluxDBClient(host='127.0.0.1', port=8086, database='solar')
  18. result = client.query("SELECT sum(\"eingespeiste_leistung\") *0.000002777777777 FROM \"Tannenstrasse\" WHERE time >= " + beginning + "ms and time <= " + end + "ms")
  19. kwh = round(result.raw['series'][0]['values'][0][1], 1)
  20. urllib.request.urlretrieve("http://localhost:3000/render/d-solo/-wCxs6mgk/solaranlage?orgId=1&refresh=10s&panelId=2&from=" + beginning + "&to=" + end + "&width=600&height=300&tz=Europe%2FBerlin", tmpdir.name + "/leistung.png")
  21. # Send an HTML email with an embedded image and a plain text message for
  22. # email clients that don't want to display the HTML.
  23. from email.mime.multipart import MIMEMultipart
  24. from email.mime.text import MIMEText
  25. from email.mime.image import MIMEImage
  26. # Define these once; use them twice!
  27. strFrom = 'pv-tannenstr@web.de'
  28. strTo = ["tobias.siegel@outlook.com", "wsiegel@web.de"]
  29. # Create the root message and fill in the from, to, and subject headers
  30. msgRoot = MIMEMultipart('related')
  31. msgRoot['Subject'] = datetime.today().strftime('%Y-%m-%d') + ' - ' + str(kwh) + 'kWh - Solaranlage'
  32. msgRoot['From'] = strFrom
  33. msgRoot['To'] = ",".join(strTo)
  34. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  35. # Encapsulate the plain and HTML versions of the message body in an
  36. # 'alternative' part, so message agents can decide which they want to display.
  37. msgAlternative = MIMEMultipart('alternative')
  38. msgRoot.attach(msgAlternative)
  39. msgText = MIMEText('This is the alternative plain text message.')
  40. msgAlternative.attach(msgText)
  41. # Get Boot Time and Convert it to String
  42. bt = get_boot_time()
  43. bt_str = bt.strftime("%d.%m.%Y, %H.%M")
  44. # We reference the image in the IMG SRC attribute by the ID we give it dasdasdbelow
  45. msgText = MIMEText('<img src="cid:image1"><br><p>Letzter Start: ' + bt_str + ' Uhr</p>', 'html')
  46. msgAlternative.attach(msgText)
  47. # This example assumes the image is in the current directory
  48. fp = open(tmpdir.name + '/leistung.png', 'rb')
  49. msgImage = MIMEImage(fp.read())
  50. fp.close()
  51. # Define the image's ID as referenced above
  52. msgImage.add_header('Content-ID', '<image1>')
  53. msgRoot.attach(msgImage)
  54. # Send the email (this example assumes SMTP authentication is required)
  55. import smtplib, ssl
  56. context = ssl.create_default_context()
  57. with smtplib.SMTP('smtp.web.de', 587) as server:
  58. server.ehlo() # Can be omitted
  59. server.starttls(context=context)
  60. server.ehlo() # Can be omitted
  61. server.login(strFrom, 'PV-Tannenstr')
  62. server.sendmail(strFrom, strTo, msgRoot.as_string())
  63. tmpdir.cleanup()