Add sum Items, clean UP
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
import sys
|
||||
import socket
|
||||
import subprocess
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
import smtplib
|
||||
import os
|
||||
from email.message import EmailMessage
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# Define server details
|
||||
host = "front"
|
||||
port = 993
|
||||
|
||||
# Function to send an email notification
|
||||
def send_email(subject, body, to_email):
|
||||
smtp_server = os.getenv('SMTP_SERVER')
|
||||
smtp_port = int(os.getenv('SMTP_PORT', 587))
|
||||
smtp_user = os.getenv('SMTP_USER')
|
||||
smtp_password = os.getenv('SMTP_PASSWORD')
|
||||
from_email = os.getenv('FROM_EMAIL')
|
||||
|
||||
msg = EmailMessage()
|
||||
msg.set_content(body)
|
||||
msg['Subject'] = subject
|
||||
msg['From'] = from_email
|
||||
msg['To'] = to_email
|
||||
|
||||
try:
|
||||
with smtplib.SMTP(smtp_server, smtp_port) as server:
|
||||
server.starttls()
|
||||
server.login(smtp_user, smtp_password)
|
||||
server.send_message(msg)
|
||||
print(f"Error notification sent to {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
|
||||
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()
|
||||
Reference in New Issue
Block a user