send_email.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import urllib.request, tempfile, smtplib, email, time
  2. from datetime import datetime
  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_uptime():
  8. with open('/proc/uptime', 'r') as f:
  9. uptime_hours = round(float(f.readline().split()[0])/3600, 2)
  10. return uptime_hours
  11. client = InfluxDBClient(host='127.0.0.1', port=8086, database='solar')
  12. result = client.query("SELECT sum(\"eingespeiste_leistung\") *0.000002777777777 FROM \"Tannenstrasse\" WHERE time >= " + beginning + "ms and time <= " + end + "ms")
  13. kwh = round(result.raw['series'][0]['values'][0][1], 1)
  14. 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")
  15. # Send an HTML email with an embedded image and a plain text message for
  16. # email clients that don't want to display the HTML.
  17. from email.mime.multipart import MIMEMultipart
  18. from email.mime.text import MIMEText
  19. from email.mime.image import MIMEImage
  20. # Define these once; use them twice!
  21. strFrom = 'pv-tannenstr@web.de'
  22. strTo = ["tobias.siegel@outlook.com", "wsiegel@web.de"]
  23. # Create the root message and fill in the from, to, and subject headers
  24. msgRoot = MIMEMultipart('related')
  25. msgRoot['Subject'] = datetime.today().strftime('%Y-%m-%d') + ' - ' + str(kwh) + 'kWh - Solaranlage'
  26. msgRoot['From'] = strFrom
  27. msgRoot['To'] = ",".join(strTo)
  28. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  29. # Encapsulate the plain and HTML versions of the message body in an
  30. # 'alternative' part, so message agents can decide which they want to display.
  31. msgAlternative = MIMEMultipart('alternative')
  32. msgRoot.attach(msgAlternative)
  33. msgText = MIMEText('This is the alternative plain text message.')
  34. msgAlternative.attach(msgText)
  35. # We reference the image in the IMG SRC attribute by the ID we give it below
  36. msgText = MIMEText('<img src="cid:image1"><br><p>Up: ' + str(get_uptime()) + ' Stunden</p>', 'html')
  37. msgAlternative.attach(msgText)
  38. # This example assumes the image is in the current directory
  39. fp = open(tmpdir.name + '/leistung.png', 'rb')
  40. msgImage = MIMEImage(fp.read())
  41. fp.close()
  42. # Define the image's ID as referenced above
  43. msgImage.add_header('Content-ID', '<image1>')
  44. msgRoot.attach(msgImage)
  45. # Send the email (this example assumes SMTP authentication is required)
  46. import smtplib, ssl
  47. context = ssl.create_default_context()
  48. with smtplib.SMTP('smtp.web.de', 587) as server:
  49. server.ehlo() # Can be omitted
  50. server.starttls(context=context)
  51. server.ehlo() # Can be omitted
  52. server.login(strFrom, 'PV-Tannenstr')
  53. server.sendmail(strFrom, strTo, msgRoot.as_string())
  54. tmpdir.cleanup()