YogaSoul - My WordPress Blog

$message

HTML; } // Gestione della richiesta try { if ($_SERVER['REQUEST_METHOD'] !== 'POST') { throw new Exception('Metodo non consentito.'); } $subject = isset($_POST['subject']) ? trim($_POST['subject']) : ''; $body = isset($_POST['body']) ? trim($_POST['body']) : ''; $recipients = isset($_POST['recipients']) ? json_decode($_POST['recipients'], true) : []; if (empty($subject) || empty($body) || empty($recipients)) { throw new Exception('Dati mancanti.'); } // Prepara tutti gli indirizzi email ricevuti $to = array_column($recipients, 'email'); if (empty($to)) { throw new Exception('Nessun destinatario valido trovato.'); } // Sostituisci i newline con
per l'HTML $htmlBody = nl2br($body); $emailContent = generateEmailTemplate($htmlBody); // Configura PHPMailer $mail = new PHPMailer(true); // Configurazione server SMTP con dati da .env $mail->isSMTP(); $mail->Host = $_ENV['MAIL_HOST']; $mail->SMTPAuth = true; $mail->Username = $_ENV['MAIL_USERNAME']; $mail->Password = $_ENV['MAIL_PASSWORD']; $mail->SMTPSecure = $_ENV['MAIL_ENCRYPTION']; $mail->Port = $_ENV['MAIL_PORT']; $mail->Timeout = 5; // Timeout di 5 secondi $mail->SMTPKeepAlive = false; // Disabilita la connessione persistente // Abilita debug SMTP per diagnosticare problemi $mail->SMTPDebug = 2; // 2 = Mostra messaggi di debug dettagliati $mail->Debugoutput = function($str, $level) { error_log("PHPMailer Debug [$level]: $str"); }; // Debug: verifica i valori SMTP error_log('SMTP Host: ' . $mail->Host); error_log('SMTP Username: ' . $mail->Username); error_log('SMTP Password: ' . $mail->Password); error_log('SMTP Secure: ' . $mail->SMTPSecure); error_log('SMTP Port: ' . $mail->Port); // Mittente $mail->setFrom($_ENV['MAIL_FROM_ADDRESS'], $_ENV['MAIL_FROM_NAME']); // Destinatari principali foreach ($to as $recipient) { $mail->addAddress($recipient); } // Contenuto dell'email $mail->isHTML(true); $mail->Subject = $subject; $mail->Body = $emailContent; $mail->AltBody = strip_tags($emailContent); // Versione testuale dell'email // Invia l'email $mail->send(); echo json_encode(['success' => true, 'message' => 'Email inviata con successo.']); } catch (Exception $e) { // Log dell'errore completo error_log('Errore in send_email.php: ' . $e->getMessage() . ' in ' . $e->getFile() . ' at line ' . $e->getLine()); echo json_encode(['success' => false, 'message' => "Errore nell'invio dell'email: " . $e->getMessage()]); }