some changes
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
SMTP_SERVER=mail.itdata-gera.de
|
||||
SMTP_PORT=587
|
||||
SMTP_USER=serfling@itdata-gera.de
|
||||
SMTP_PASSWORD="xPElLoyD,94,#"
|
||||
FROM_EMAIL=serfling@itdata-gera.de
|
||||
TO_EMAIL=serfling@itdata-gera.de
|
||||
+105
-91
@@ -7,11 +7,12 @@ import smtplib
|
||||
import os
|
||||
from email.message import EmailMessage
|
||||
from dotenv import load_dotenv
|
||||
import time # Import time module for sleep functionality
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# Define server details
|
||||
host = "front"
|
||||
host = "180.1.1.164"
|
||||
port = 993
|
||||
|
||||
# Function to send an email notification
|
||||
@@ -37,99 +38,112 @@ def send_email(subject, body, to_email):
|
||||
except Exception as e:
|
||||
print(f"Failed to send email: {e}")
|
||||
|
||||
# Check if mail server is active
|
||||
try:
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.settimeout(1)
|
||||
result = sock.connect_ex((host, port))
|
||||
if result != 0:
|
||||
print(f"Fehler beim Verbinden zu {host} auf Port {port}") # --- E-Mail senden
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Verbinden zu {host} auf Port {port}: {e}") # --- E-Mail senden
|
||||
finally:
|
||||
sock.close()
|
||||
|
||||
# Prepare SQLite database connection
|
||||
db_path = "imapsync_results.db"
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Create tables if not exists
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS sync_results (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
email TEXT,
|
||||
messages_transferred INTEGER,
|
||||
date TEXT
|
||||
)
|
||||
''')
|
||||
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
email TEXT UNIQUE,
|
||||
password TEXT,
|
||||
domain TEXT
|
||||
)
|
||||
''')
|
||||
|
||||
# Fetch user data from the SQLite database
|
||||
cursor.execute("SELECT email, password, domain FROM users")
|
||||
users = cursor.fetchall()
|
||||
|
||||
if not users:
|
||||
print("No users found in the database.")
|
||||
sys.exit(1)
|
||||
|
||||
# Process each user
|
||||
for user in users:
|
||||
username, password, domain = user
|
||||
local_part = username.split('@')[0]
|
||||
|
||||
# Command for imapsync
|
||||
command = [
|
||||
"imapsync",
|
||||
"--host1", domain,
|
||||
"--user1", username,
|
||||
"--password1", password,
|
||||
"--host2", "180.1.1.164",
|
||||
"--user2", "archiv@archiv.trendsetzer.eu",
|
||||
"--password2", "pass",
|
||||
"--subfolder2", f"{local_part}",
|
||||
"--regextrans2", "s/^(.*)$/\\1/",
|
||||
"--nolog"
|
||||
]
|
||||
|
||||
# Run the command and capture the output
|
||||
# Start the infinite loop
|
||||
while True:
|
||||
try:
|
||||
result = subprocess.run(command, capture_output=True, text=True)
|
||||
output = result.stdout
|
||||
# Check if mail server is active
|
||||
try:
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.settimeout(1)
|
||||
result = sock.connect_ex((host, port))
|
||||
if result != 0:
|
||||
print(f"Fehler beim Verbinden zu {host} auf Port {port}") # --- E-Mail senden
|
||||
send_email("Server Connection Error", f"Fehler beim Verbinden zu {host} auf Port {port}", os.getenv('TO_EMAIL'))
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Verbinden zu {host} auf Port {port}: {e}") # --- E-Mail senden
|
||||
send_email("Server Connection Error", f"Fehler beim Verbinden zu {host} auf Port {port}: {e}", os.getenv('TO_EMAIL'))
|
||||
finally:
|
||||
sock.close()
|
||||
|
||||
# Check for EXIT_AUTHENTICATION_FAILURE_USER error
|
||||
if "EXIT_AUTHENTICATION_FAILURE_USER" in output:
|
||||
subject = f"Authentication Failure for {username}"
|
||||
body = f"An authentication failure occurred for user {username} during IMAP sync."
|
||||
send_email(subject, body, os.getenv('TO_EMAIL'))
|
||||
# Prepare SQLite database connection
|
||||
db_path = "/app/db/imapsync_results.db"
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Find and extract the "Messages transferred" line
|
||||
for line in output.splitlines():
|
||||
if "Messages transferred" in line:
|
||||
transferred_messages = line.split(":")[-1].strip()
|
||||
transferred_count = int(transferred_messages)
|
||||
current_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
# Create tables if not exists
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS sync_results (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
email TEXT,
|
||||
messages_transferred INTEGER,
|
||||
date TEXT
|
||||
)
|
||||
''')
|
||||
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
email TEXT UNIQUE,
|
||||
password TEXT,
|
||||
domain TEXT
|
||||
)
|
||||
''')
|
||||
|
||||
# Fetch user data from the SQLite database
|
||||
cursor.execute("SELECT email, password, domain FROM users")
|
||||
users = cursor.fetchall()
|
||||
|
||||
if not users:
|
||||
print("No users found in the database.")
|
||||
sys.exit(1)
|
||||
|
||||
# Process each user
|
||||
for user in users:
|
||||
username, password, domain = user
|
||||
local_part = username.split('@')[0]
|
||||
print(f"User: {username} running.")
|
||||
# Command for imapsync
|
||||
command = [
|
||||
"imapsync",
|
||||
"--host1", domain,
|
||||
"--user1", username,
|
||||
"--password1", password,
|
||||
"--host2", "180.1.1.164",
|
||||
"--user2", "archiv@trendsetzer.eu",
|
||||
"--password2", "Ln0m2YQZd23H54L5tCiyjIBWLEn8mk36v7KauqS8QFGzu",
|
||||
"--subfolder2", f"{local_part}",
|
||||
"--regextrans2", "s/^(.*)$/\\1/",
|
||||
"--log",
|
||||
"--notls2"
|
||||
]
|
||||
|
||||
# Run the command and capture the output
|
||||
try:
|
||||
result = subprocess.run(command, capture_output=True, text=True)
|
||||
output = result.stdout
|
||||
|
||||
# Check for EXIT_AUTHENTICATION_FAILURE_USER error
|
||||
if "EXIT_AUTHENTICATION_FAILURE_USER" in output:
|
||||
subject = f"Authentication Failure for {username}"
|
||||
body = f"An authentication failure occurred for user {username} during IMAP sync."
|
||||
send_email(subject, body, os.getenv('TO_EMAIL'))
|
||||
|
||||
# Find and extract the "Messages transferred" line
|
||||
for line in output.splitlines():
|
||||
if "Messages transferred" in line:
|
||||
transferred_messages = line.split(":")[-1].strip()
|
||||
transferred_count = int(transferred_messages)
|
||||
current_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# Insert result into SQLite database
|
||||
cursor.execute('''
|
||||
INSERT INTO sync_results (email, messages_transferred, date)
|
||||
VALUES (?, ?, ?)
|
||||
''', (username, transferred_count, current_date))
|
||||
conn.commit()
|
||||
print(f"Data inserted for {username}: {transferred_count} messages on {current_date}")
|
||||
break
|
||||
else:
|
||||
print(f"No 'Messages transferred' line found for {username}.")
|
||||
except Exception as e:
|
||||
print(f"Error running imapsync for {username}: {e}")
|
||||
|
||||
# Close database connection
|
||||
conn.close()
|
||||
|
||||
# Insert result into SQLite database
|
||||
cursor.execute('''
|
||||
INSERT INTO sync_results (email, messages_transferred, date)
|
||||
VALUES (?, ?, ?)
|
||||
''', (username, transferred_count, current_date))
|
||||
conn.commit()
|
||||
print(f"Data inserted for {username}: {transferred_count} messages on {current_date}")
|
||||
break
|
||||
else:
|
||||
print(f"No 'Messages transferred' line found for {username}.")
|
||||
except Exception as e:
|
||||
print(f"Error running imapsync for {username}: {e}")
|
||||
print(f"An error occurred during the loop execution: {e}")
|
||||
|
||||
# Close database connection
|
||||
conn.close()
|
||||
# Sleep for 5 minutes before the next iteration
|
||||
print("Sleep 5 minutes")
|
||||
time.sleep(300)
|
||||
|
||||
@@ -40,7 +40,7 @@ def main():
|
||||
view_users = st.sidebar.button("View Users", key="view_users_button")
|
||||
delete_user = st.sidebar.button("Delete User", key="delete_user_button")
|
||||
st.sidebar.markdown(
|
||||
'<a href="http://180.1.1.164:8000" target="_blank"><button style="background-color: LightGray; color: black; border: none; padding: 8px 16px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer;">View Mails</button></a>',
|
||||
'<a href="http://180.1.1.164" target="_blank"><button style="background-color: LightGray; color: black; border: none; padding: 8px 16px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer;">View Mails</button></a>',
|
||||
unsafe_allow_html=True
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user