send_email_restart.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import urllib.request, tempfile, smtplib, email, time
  2. from datetime import datetime, timedelta
  3. tmpdir = tempfile.TemporaryDirectory()
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.text import MIMEText
  6. from email.mime.image import MIMEImage
  7. # Define these once; use them twice!
  8. strFrom = 'pv-tannenstr@web.de'
  9. strTo = ["tobias.siegel@outlook.com", "wsiegel@web.de"]
  10. # Create the root message and fill in the from, to, and subject headers
  11. msgRoot = MIMEMultipart('related')
  12. msgRoot['Subject'] = 'Darmstadt Neustart RPI'
  13. msgRoot['From'] = strFrom
  14. msgRoot['To'] = ",".join(strTo)
  15. msgRoot.preamble = 'This is a multi-part message in MIME format.'
  16. # Encapsulate the plain and HTML versions of the message body in an
  17. # 'alternative' part, so message agents can decide which they want to display.
  18. msgAlternative = MIMEMultipart('alternative')
  19. msgRoot.attach(msgAlternative)
  20. msgText = MIMEText('')
  21. msgAlternative.attach(msgText)
  22. # Send asdthe email (this example assumes SMTP authentication is required)
  23. import smtplib, ssl
  24. context = ssl.create_default_context()
  25. with smtplib.SMTP('smtp.web.de', 587) as server:
  26. server.ehlo() # Can be omitted
  27. server.starttls(context=context)
  28. server.ehlo() # Can be omitted
  29. server.login(strFrom, 'PV-Tannenstr')
  30. server.sendmail(strFrom, strTo, msgRoot.as_string())
  31. tmpdir.cleanup()