email cert notifiche
This commit is contained in:
+63
-33
@@ -8,6 +8,7 @@ use PHPMailer\PHPMailer\Exception;
|
||||
|
||||
// Imposta l'header per JSON
|
||||
header('Content-Type: application/json');
|
||||
ini_set('display_errors', '0'); // gli avvisi vanno nel log, non nella risposta
|
||||
|
||||
// Funzione per generare il template HTML dell'email
|
||||
function generateEmailTemplate($message)
|
||||
@@ -40,7 +41,7 @@ function generateEmailTemplate($message)
|
||||
text-align: center;
|
||||
}
|
||||
.header img {
|
||||
max-width: 50px; /* Ridotto da 150px a 100px per rimpicciolire il logo */
|
||||
max-width: 100px; /* Ridotto da 150px a 100px per rimpicciolire il logo */
|
||||
}
|
||||
.content {
|
||||
padding: 30px;
|
||||
@@ -77,8 +78,8 @@ function generateEmailTemplate($message)
|
||||
<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;">
|
||||
width="120"
|
||||
style="display: block; width: 120px; max-width: 120px; height: auto; margin: 0 auto;">
|
||||
|
||||
</div>
|
||||
<div class="content">
|
||||
@@ -95,6 +96,16 @@ function generateEmailTemplate($message)
|
||||
HTML;
|
||||
}
|
||||
|
||||
// Trasforma il testo del modulo in HTML:
|
||||
// **testo** -> grassetto, link cliccabili, a capo -> <br>
|
||||
function formatEmailBody(string $text): string
|
||||
{
|
||||
$html = htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
|
||||
$html = preg_replace('/\*\*([^*\n]+?)\*\*/', '<strong>$1</strong>', $html);
|
||||
$html = preg_replace('~(https?://[^\s<]+)~', '<a href="$1">$1</a>', $html);
|
||||
return nl2br($html);
|
||||
}
|
||||
|
||||
// Gestione della richiesta
|
||||
try {
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
@@ -105,26 +116,15 @@ try {
|
||||
$body = isset($_POST['body']) ? trim($_POST['body']) : '';
|
||||
$recipients = isset($_POST['recipients']) ? json_decode($_POST['recipients'], true) : [];
|
||||
|
||||
if (empty($subject) || empty($body) || empty($recipients)) {
|
||||
if (empty($subject) || empty($body) || empty($recipients) || !is_array($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);
|
||||
// Invii multipli: più tempo a disposizione dello script
|
||||
@set_time_limit(300);
|
||||
|
||||
// Configura PHPMailer
|
||||
$mail = new PHPMailer(true);
|
||||
|
||||
// Configurazione server SMTP con dati da .env
|
||||
$mail->isSMTP();
|
||||
$mail->Host = $_ENV['MAIL_HOST'];
|
||||
$mail->SMTPAuth = true;
|
||||
@@ -132,29 +132,59 @@ try {
|
||||
$mail->Password = $_ENV['MAIL_PASSWORD'];
|
||||
$mail->SMTPSecure = $_ENV['MAIL_ENCRYPTION'];
|
||||
$mail->Port = (int) $_ENV['MAIL_PORT'];
|
||||
$mail->Timeout = 5; // Timeout di 5 secondi
|
||||
$mail->SMTPKeepAlive = false; // Disabilita la connessione persistente
|
||||
$mail->Timeout = 5;
|
||||
$mail->SMTPKeepAlive = true; // una sola connessione per tutti gli invii
|
||||
$mail->CharSet = 'UTF-8'; // accenti ed emoji corretti
|
||||
|
||||
// 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
|
||||
$sent = 0;
|
||||
$failed = [];
|
||||
|
||||
// Un'email per ogni destinatario: nessuno vede gli indirizzi degli altri
|
||||
foreach ($recipients as $r) {
|
||||
$email = trim($r['email'] ?? '');
|
||||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
$failed[] = $email !== '' ? $email : '(email vuota)';
|
||||
continue;
|
||||
}
|
||||
|
||||
$nome = trim((string) ($r['nome'] ?? ''));
|
||||
if ($nome === '') {
|
||||
$nome = 'allieva/o';
|
||||
}
|
||||
|
||||
$personalText = str_replace('{nome}', $nome, $body);
|
||||
|
||||
try {
|
||||
$mail->clearAddresses();
|
||||
$mail->addAddress($email, $nome);
|
||||
$mail->Body = generateEmailTemplate(formatEmailBody($personalText));
|
||||
$mail->AltBody = str_replace('**', '', $personalText);
|
||||
$mail->send();
|
||||
|
||||
echo json_encode(['success' => true, 'message' => 'Email inviata con successo.']);
|
||||
$sent++;
|
||||
} catch (Exception $e) {
|
||||
// Log dell'errore completo
|
||||
$failed[] = $email;
|
||||
error_log('Errore invio a ' . $email . ': ' . $mail->ErrorInfo);
|
||||
$mail->getSMTPInstance()->reset();
|
||||
}
|
||||
}
|
||||
|
||||
$mail->smtpClose();
|
||||
|
||||
if ($sent === 0) {
|
||||
throw new Exception('Nessuna email inviata.');
|
||||
}
|
||||
|
||||
$message = 'Email inviate: ' . $sent;
|
||||
if (!empty($failed)) {
|
||||
$message .= '. Non inviate (' . count($failed) . '): ' . implode(', ', $failed);
|
||||
}
|
||||
|
||||
echo json_encode(['success' => true, 'message' => $message]);
|
||||
} catch (\Throwable $e) {
|
||||
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()]);
|
||||
}
|
||||
|
||||
+166
-819
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user