send_mail.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. if 'series' in result.raw:
  20. kwh = round(result.raw['series'][0]['values'][0][1], 1)
  21. else:
  22. kwh = "0 - Keine Werte"
  23. 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")
  24. # Send an HTML email with an embedded image and a plain text message for
  25. # email clients that don't want to display the HTML.
  26. from email.mime.multipart import MIMEMultipart
  27. from email.mime.text import MIMEText
  28. from email.mime.image import MIMEImage
  29. # Define these once; use them twice!
  30. strFrom = 'pv-tannenstr@web.de'
  31. strTo = ["tobias.siegel@outlook.com", "wsiegel@web.de"]
  32. # Create the root message and fill in the from, to, and subject headers
  33. msgRoot = MIMEMultipart('related')
  34. msgRoot['Subject'] = datetime.today().strftime('%Y-%m-%d') + ' - ' + str(kwh) + 'kWh - Solaranlage'
  35. msgRoot['From'] = strFrom
  36. msgRoot['To'] = ",".join(strTo)
  37. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  38. # Encapsulate the plain and HTML versions of the message body in an
  39. # 'alternative' part, so message agents can decide which they want to display.
  40. msgAlternative = MIMEMultipart('alternative')
  41. msgRoot.attach(msgAlternative)
  42. msgText = MIMEText('This is the alternative plain text message.')
  43. msgAlternative.attach(msgText)
  44. # Get Boot Time and Convert it to String
  45. bt = get_boot_time()
  46. bt_str = bt.strftime("%d.%m.%Y, %H.%M")
  47. # We reference the image in the IMG SRC attribute by the ID we give it dasdasdbelow
  48. msgText = MIMEText('<img src="cid:image1"><br><p>Letzter Start: ' + bt_str + ' Uhr</p>', 'html')
  49. msgAlternative.attach(msgText)
  50. # This example assumes the image is in the current directory
  51. fp = open(tmpdir.name + '/leistung.png', 'rb')
  52. msgImage = MIMEImage(fp.read())
  53. fp.close()
  54. # Define the image's ID as referenced above
  55. msgImage.add_header('Content-ID', '<image1>')
  56. msgRoot.attach(msgImage)
  57. # Send the email (this example assumes SMTP authentication is required)
  58. import smtplib, ssl
  59. context = ssl.create_default_context()
  60. with smtplib.SMTP('smtp.web.de', 587) as server:
  61. server.ehlo() # Can be omitted
  62. server.starttls(context=context)
  63. server.ehlo() # Can be omitted
  64. server.login(strFrom, 'PV-Tannenstr')
  65. server.sendmail(strFrom, strTo, msgRoot.as_string())
  66. tmpdir.cleanup()