172 lines
5.2 KiB
PHP
172 lines
5.2 KiB
PHP
<?php
|
|
// Includi il file mailer.php per caricare le dipendenze e le variabili .env
|
|
require_once 'class/mailer.php';
|
|
|
|
// Importa le classi necessarie
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
// Imposta l'header per JSON
|
|
header('Content-Type: application/json');
|
|
|
|
// Funzione per generare il template HTML dell'email
|
|
function generateEmailTemplate($message) {
|
|
return <<<HTML
|
|
<!DOCTYPE html>
|
|
<html lang="it">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Email da YogiBook</title>
|
|
<style>
|
|
body {
|
|
font-family: 'Arial', sans-serif;
|
|
background-color: #f4f4f4;
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
.container {
|
|
max-width: 600px;
|
|
margin: 20px auto;
|
|
background-color: #ffffff;
|
|
border-radius: 10px;
|
|
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
|
overflow: hidden;
|
|
}
|
|
.header {
|
|
background-color: #e6f0fa;
|
|
padding: 20px;
|
|
text-align: center;
|
|
}
|
|
.header img {
|
|
max-width: 50px; /* Ridotto da 150px a 100px per rimpicciolire il logo */
|
|
}
|
|
.content {
|
|
padding: 30px;
|
|
color: #333333;
|
|
line-height: 1.6;
|
|
}
|
|
.content p {
|
|
margin-bottom: 15px;
|
|
}
|
|
.footer {
|
|
background-color: #e6f0fa;
|
|
padding: 15px;
|
|
text-align: center;
|
|
font-size: 14px;
|
|
color: #666666;
|
|
}
|
|
.footer a {
|
|
color: #4a90e2;
|
|
text-decoration: none;
|
|
}
|
|
.footer a:hover {
|
|
text-decoration: underline;
|
|
}
|
|
.yoga-decoration {
|
|
width: 100%;
|
|
height: 10px;
|
|
background: linear-gradient(to right, #4a90e2, #87ceeb);
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="yoga-decoration"></div>
|
|
<div class="header">
|
|
<img src="https://yogasoul.it/wp-content/uploads/2021/03/logotransizione_final2.png"
|
|
alt="YogaSoul - My WordPress Blog"
|
|
width="150"
|
|
style="display: block; width: 50px; height: auto;">
|
|
|
|
</div>
|
|
<div class="content">
|
|
<p>$message</p>
|
|
</div>
|
|
<div class="footer">
|
|
<p>Ti aspettiamo alla tua prossima lezione di yoga!</p>
|
|
<p><a href="https://yogasoul.it">YogaSoul</a> | <a href="mailto:info@yogasoul.it">Contattaci</a></p>
|
|
</div>
|
|
<div class="yoga-decoration"></div>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
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 <br> 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()]);
|
|
} |