Update checking.py

• Codes have been simplified and optimized.
• E-Mail contents were assigned to variables. (For more language support)
• Description lines should be made more easily understandable.
This commit is contained in:
Koray Üstündağ
2023-10-03 20:49:53 +03:00
committed by GitHub
parent d388cda592
commit 9a9299ae7b
+40 -65
View File
@@ -5,55 +5,59 @@ from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
# Necessary files. You should create these.
checkin_file = "./checkin_file.txt" # replace with your checkin file path
counter_file = "./counter_file.txt" # this is needed to stop spamming mails to the loved ones
count_sent_mail = 7
# Configuration
smtp_host = 'your_smpt_server' # SMTP server hostname
smtp_port = 587 # SMTP server port
smtp_user = 'username' # SMTP server username
smtp_password = 'password' # SMTP server password
from_email = 'coming_from' # Sender's email address
to_email = 'going_to' # Recipient's email address
checkin_file = "./checkin_file.txt" # File path for check-in timestamp
counter_file = "./counter_file.txt" # File path for email counter
DAYS_REMINDER = 25 # Days for the reminder period
DAYS_DEADMAN = 30 # Days for the dead man's switch
SECONDS_IN_A_DAY = 86400 # Number of seconds in a day
count_sent_mail = 7 # Number of reminder emails to send
DAYS_REMINDER = 25 # Modify these as per your need
DAYS_DEADMAN = 30 # Modify these as per your need
SECONDS_IN_A_DAY = 86400 # No need to modify this
family_members = ['[email protected]'] # List of family members' email addresses
# Replace these with your SMTP settings and email
smtp_host = 'your_smpt_server'
smtp_port = 587
smtp_user = 'username'
smtp_password = 'password'
# Email content
reminder_subject = "REMINDER" # Subject for the reminder email
reminder_message = "Please check-in" # Message for the reminder email
# Reminder message
from_email = 'coming_from'
to_email = 'going_to'
dead_man_activation_subject = "Dead Man's Switch Activated" # Subject for the activation email
dead_man_activation_message = "You forgot to check-in" # Message for the activation email
#Dead man message
family_members = ['[email protected]'] # You can specify multiple email ids separated by commas
message = "I'm dead"
files_to_attach = ['file.gpg'] # specify your files
dead_man_subject = "I Am Gone"
dead_man_subject = "I Am Gone" # Subject for the dead man's switch email
dead_man_message = "I'm dead" # Message for the dead man's switch email
files_to_attach = ['file.gpg'] # List of files to attach
def send_email(subject, message):
# Setup smtp
# Function to send an email
def send_email(subject, message, to, attachments=[]):
s = smtplib.SMTP(host=smtp_host, port=smtp_port)
s.starttls()
s.login(smtp_user, smtp_password)
# Create message
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = to_email
msg['To'] = to
msg['Subject'] = subject
# Add in the message body
msg.attach(MIMEText(message, 'plain'))
# Send the email
s.send_message(msg)
del msg
for file in attachments:
with open(file, 'rb') as file_in:
part = MIMEBase('application', 'octet-stream')
part.set_payload(file_in.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f"attachment; filename={file}")
msg.attach(part)
s.send_message(msg)
s.quit()
# Function to execute the dead man's switch
def execute_deadman_switch():
# check if counter file exists
try:
with open(counter_file, "r") as file:
counter = int(file.read())
@@ -61,38 +65,12 @@ def execute_deadman_switch():
counter = 0
if counter < count_sent_mail:
s = smtplib.SMTP(host=smtp_host, port=smtp_port)
s.starttls()
s.login(smtp_user, smtp_password)
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = ', '.join(family_members)
msg['Subject'] = dead_man_subject
msg.attach(MIMEText(message, 'plain'))
# Attach multiple files
for file in files_to_attach:
with open(file, 'rb') as file_in:
part = MIMEBase('application', 'octet-stream')
part.set_payload(file_in.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f"attachment; filename= {file}")
msg.attach(part)
# Send the email
s.send_message(msg)
del msg
s.quit()
# increment counter and write to file
send_email(dead_man_subject, dead_man_message, ', '.join(family_members), files_to_attach)
with open(counter_file, "w") as file:
file.write(str(counter + 1))
file.write(str(counter + 1))
# Function to check the dead man's switch
def check_deadman():
with open(checkin_file, "r") as file:
last_checkin_time = float(file.read().strip())
@@ -100,14 +78,11 @@ def check_deadman():
time_difference = time.time() - last_checkin_time
if time_difference > DAYS_REMINDER * SECONDS_IN_A_DAY:
if time_difference > DAYS_DEADMAN * SECONDS_IN_A_DAY:
send_email("Dead Man's Switch Activated", "You forgot to check-in")
send_email(dead_man_activation_subject, dead_man_activation_message, to_email)
execute_deadman_switch()
else:
send_email("REMINDER", "Please check-in")
send_email(reminder_subject, reminder_message, to_email)
if __name__ == '__main__':
check_deadman()