fix send mail with python script

This commit is contained in:
Francesco Picone
2025-12-09 17:35:40 +01:00
parent b79b31df69
commit 568a02251c
5 changed files with 296 additions and 13 deletions

View File

@@ -74,7 +74,9 @@ define('MAIL_FROM_NAME', 'Pilates Studio');
define('USE_SMTP', true); // Imposta false per usare mail() PHP di base
// Impostazioni SMTP - Gmail
// NOTA: Le email vengono inviate tramite script Python (send_email.py) per maggiore affidabilità
// Per Gmail: usa password applicazione da https://myaccount.google.com/apppasswords
// Requisito: Python 3.6+ installato sul server (testa con: python3 --version)
define('SMTP_HOST', 'smtp.gmail.com');
define('SMTP_PORT', 587);
define('SMTP_USERNAME', 'pyco.networking@gmail.com'); // Inserisci la tua email Gmail

View File

@@ -433,7 +433,7 @@ function send_email($to, $subject, $message) {
}
/**
* Invia email tramite SMTP (Gmail)
* Invia email tramite script Python
*
* @param string $to Email destinatario
* @param string $subject Oggetto email
@@ -441,6 +441,53 @@ function send_email($to, $subject, $message) {
* @return bool True se inviata, false altrimenti
*/
function send_smtp_email($to, $subject, $message) {
try {
// Prepara dati JSON per lo script Python
$data = json_encode([
'to' => $to,
'subject' => $subject,
'html' => $message
], JSON_UNESCAPED_UNICODE);
// Percorso dello script Python
$script_path = __DIR__ . '/../send_email.py';
// Verifica che lo script esista
if (!file_exists($script_path)) {
error_log("Email Error: Script send_email.py non trovato");
return false;
}
// Esegui script Python
$command = 'python3 ' . escapeshellarg($script_path) . ' ' . escapeshellarg($data) . ' 2>&1';
exec($command, $output, $return_code);
// Parse risposta JSON
$result = json_decode(implode("\n", $output), true);
if ($return_code === 0 && isset($result['success']) && $result['success']) {
return true;
} else {
$error_msg = isset($result['error']) ? $result['error'] : 'Errore sconosciuto';
error_log("Email Error: " . $error_msg);
return false;
}
} catch (Exception $e) {
error_log("Email Error: " . $e->getMessage());
return false;
}
}
/**
* Invia email tramite SMTP PHP nativo (deprecato - usa send_smtp_email)
*
* @param string $to Email destinatario
* @param string $subject Oggetto email
* @param string $message Corpo email (HTML)
* @return bool True se inviata, false altrimenti
*/
function send_smtp_email_legacy($to, $subject, $message) {
try {
// Connessione al server SMTP con supporto SSL/TLS
$context = stream_context_create([