add user confirmation

This commit is contained in:
Francesco Picone
2025-12-09 17:50:01 +01:00
parent 10f709a970
commit 402826b7ef
10 changed files with 611 additions and 61 deletions

View File

@@ -2,15 +2,29 @@
"""
Script Python per invio email SMTP
Legge le credenziali da config.php e invia email tramite Gmail
Log solo in caso di errore
"""
import sys
import json
import re
import smtplib
import logging
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from pathlib import Path
from datetime import datetime
# Configura logging solo per errori
log_file = Path(__file__).parent / 'logs' / 'email_errors.log'
log_file.parent.mkdir(exist_ok=True)
logging.basicConfig(
filename=str(log_file),
level=logging.ERROR,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
def parse_php_config(config_path):
"""Estrae le configurazioni SMTP dal file config.php"""
@@ -86,19 +100,25 @@ def send_email(to_email, subject, html_body, config):
}
except smtplib.SMTPAuthenticationError as e:
error_msg = f'Autenticazione SMTP fallita per {to_email}: {str(e)}'
logging.error(error_msg)
return {
'success': False,
'error': f'Autenticazione SMTP fallita: {str(e)}'
'error': error_msg
}
except smtplib.SMTPException as e:
error_msg = f'Errore SMTP invio a {to_email}: {str(e)}'
logging.error(error_msg)
return {
'success': False,
'error': f'Errore SMTP: {str(e)}'
'error': error_msg
}
except Exception as e:
error_msg = f'Errore generico invio a {to_email}: {str(e)}'
logging.error(error_msg)
return {
'success': False,
'error': f'Errore generico: {str(e)}'
'error': error_msg
}
def main():
@@ -115,9 +135,11 @@ def main():
data = json.loads(sys.argv[1])
if 'to' not in data or 'subject' not in data or 'html' not in data:
error = 'Parametri mancanti: to, subject, html sono obbligatori'
logging.error(error)
print(json.dumps({
'success': False,
'error': 'Parametri mancanti: to, subject, html sono obbligatori'
'error': error
}))
sys.exit(1)
@@ -133,15 +155,19 @@ def main():
sys.exit(0 if result['success'] else 1)
except json.JSONDecodeError as e:
error = f'JSON non valido: {str(e)}'
logging.error(error)
print(json.dumps({
'success': False,
'error': f'JSON non valido: {str(e)}'
'error': error
}))
sys.exit(1)
except Exception as e:
error = f'Errore: {str(e)}'
logging.error(error)
print(json.dumps({
'success': False,
'error': f'Errore: {str(e)}'
'error': error
}))
sys.exit(1)