51 lines
1.8 KiB
PHP
51 lines
1.8 KiB
PHP
<?php
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
use PHPMailer\PHPMailer\Exception as PHPMailerException;
|
|
|
|
require_once __DIR__ . '/../../../vendor/autoload.php'; // aggiusta il path se serve
|
|
|
|
class Mailer
|
|
{
|
|
public static function send(array $to, string $subject, string $htmlBody, ?string $altBody = null): array
|
|
{
|
|
$mail = new PHPMailer(true);
|
|
try {
|
|
$mail->isSMTP();
|
|
$mail->Host = env('MAIL_HOST');
|
|
$mail->SMTPAuth = true;
|
|
$mail->Username = env('MAIL_USERNAME');
|
|
$mail->Password = env('MAIL_PASSWORD');
|
|
$mail->Port = (int) env('MAIL_PORT', 465);
|
|
$enc = env('MAIL_ENCRYPTION', 'ssl');
|
|
$mail->SMTPSecure = ($enc === 'tls')
|
|
? PHPMailer::ENCRYPTION_STARTTLS
|
|
: PHPMailer::ENCRYPTION_SMTPS;
|
|
$mail->CharSet = 'UTF-8';
|
|
|
|
$mail->setFrom(env('MAIL_FROM_ADDRESS'), env('MAIL_FROM_NAME', 'SmartTRF'));
|
|
|
|
foreach ($to as $addr) {
|
|
$addr = trim((string)$addr);
|
|
if ($addr !== '' && filter_var($addr, FILTER_VALIDATE_EMAIL)) {
|
|
$mail->addAddress($addr);
|
|
}
|
|
}
|
|
if (!$mail->getToAddresses()) {
|
|
return ['ok' => false, 'error' => 'Nessun destinatario valido'];
|
|
}
|
|
|
|
$mail->isHTML(true);
|
|
$mail->Subject = $subject;
|
|
$mail->Body = $htmlBody;
|
|
$mail->AltBody = $altBody ?: strip_tags($htmlBody);
|
|
|
|
$mail->send();
|
|
return ['ok' => true, 'error' => null];
|
|
} catch (PHPMailerException $e) {
|
|
error_log('Mailer error: ' . $mail->ErrorInfo);
|
|
return ['ok' => false, 'error' => $mail->ErrorInfo];
|
|
}
|
|
}
|
|
}
|