@@ 1,7 1,8 @@
import smtplib
import os
from email.message import EmailMessage
-
+from time import sleep
+import socket
def send_email(subject, body):
# Create a text/plain message
@@ 17,9 18,22 @@ def send_email(subject, body):
s.quit()
+def try_multiple_times(fn):
+ max_tries = 10
+ num_tries = 0
+ while num_tries < max_tries - 1:
+ try:
+ fn()
+ return
+ except socket.gaierror:
+ num_tries += 1
+ sleep(60)
+ fn() # on the last try, do not catch the exception
+
+
def send_reboot_email():
subject = f'[rpi-security-cam] Reboot detected'
body = """The security camera was just turned on.
If you did not expect this, please review the camera footage."""
- send_email(subject, body)>
\ No newline at end of file
+ try_multiple_times(lambda: send_email(subject, body))<
\ No newline at end of file