send_email.py 2.6 KB

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