send_email.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import urllib.request, tempfile, smtplib, email, time
  2. from datetime import datetime
  3. tmpdir = tempfile.TemporaryDirectory()
  4. urllib.request.urlretrieve("http://localhost:3000/render/d-solo/-wCxs6mgk/solaranlage?orgId=1&refresh=10s&panelId=2&from=" + str((1000*int(time.time()))-(24*3600000)) + "&to=" + str(1000*int(time.time())) + "&width=600&height=300&tz=Europe%2FBerlin", tmpdir.name + "/leistung.png")
  5. # Send an HTML email with an embedded image and a plain text message for
  6. # email clients that don't want to display the HTML.
  7. from email.mime.multipart import MIMEMultipart
  8. from email.mime.text import MIMEText
  9. from email.mime.image import MIMEImage
  10. # Define these once; use them twice!
  11. strFrom = 'net_flix_11@outlook.de'
  12. strTo = ["tobias.siegel@outlook.com", "t.siegel91@gmail.com","wsiegel@web.de"]
  13. # Create the root message and fill in the from, to, and subject headers
  14. msgRoot = MIMEMultipart('related')
  15. msgRoot['Subject'] = datetime.today().strftime('%Y-%m-%d') + ' Solaranlage'
  16. msgRoot['From'] = strFrom
  17. msgRoot['To'] = ",".join(strTo)
  18. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  19. # Encapsulate the plain and HTML versions of the message body in an
  20. # 'alternative' part, so message agents can decide which they want to display.
  21. msgAlternative = MIMEMultipart('alternative')
  22. msgRoot.attach(msgAlternative)
  23. msgText = MIMEText('This is the alternative plain text message.')
  24. msgAlternative.attach(msgText)
  25. # We reference the image in the IMG SRC attribute by the ID we give it below
  26. msgText = MIMEText('<img src="cid:image1">', 'html')
  27. msgAlternative.attach(msgText)
  28. # This example assumes the image is in the current directory
  29. fp = open(tmpdir.name + '/leistung.png', 'rb')
  30. msgImage = MIMEImage(fp.read())
  31. fp.close()
  32. # Define the image's ID as referenced above
  33. msgImage.add_header('Content-ID', '<image1>')
  34. msgRoot.attach(msgImage)
  35. # Send the email (this example assumes SMTP authentication is required)
  36. import smtplib, ssl
  37. context = ssl.create_default_context()
  38. with smtplib.SMTP('smtp.outlook.com', 587) as server:
  39. server.ehlo() # Can be omitted
  40. server.starttls(context=context)
  41. server.ehlo() # Can be omitted
  42. server.login(strFrom, 'kathrein47')
  43. server.sendmail(strFrom, strTo, msgRoot.as_string())
  44. tmpdir.cleanup()