isSMTP();
$mail->Host = $_ENV['SMTP_HOST'] ?? 'localhost';
$mail->Port = $_ENV['SMTP_PORT'] ?? 25;
$mail->SMTPAuth = filter_var($_ENV['SMTP_AUTH'] ?? false, FILTER_VALIDATE_BOOLEAN);
$mail->Username = $_ENV['SMTP_USER'] ?? '';
$mail->Password = $_ENV['SMTP_PASS'] ?? '';
$mail->SMTPAutoTLS = filter_var($_ENV['SMTP_AUTO_TLS'] ?? true, FILTER_VALIDATE_BOOLEAN);
$encryption = strtolower(trim($_ENV['SMTP_ENCRYPTION'] ?? ''));
if ($encryption === 'ssl') {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
} elseif ($encryption === 'tls') {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
} else {
$mail->SMTPSecure = '';
}
$fromEmail = $_ENV['SMTP_FROM_EMAIL'] ?? 'no-reply@localhost';
$fromName = $_ENV['SMTP_FROM_NAME'] ?? 'Wizdom Mailer';
$mail->setFrom($fromEmail, $fromName);
$mail->SMTPOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
];
}
private static function parseRecipients(string $rawList): array
{
$emails = explode(',', $rawList);
$validEmails = [];
foreach ($emails as $email) {
$email = trim($email);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$validEmails[] = $email;
}
}
return $validEmails;
}
public static function send(string $to, string $subject, string $body): bool
{
try {
$mail = self::getMailer();
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->isHTML(true);
return $mail->send();
} catch (\Throwable $e) {
Logger::error("Email send failed to $to: " . $e->getMessage());
return false;
}
}
private static function buildContactHtmlBody(array $data): string
{
return "
Name: {$data['first_name']} {$data['last_name']}
Email: {$data['email']}
Phone: {$data['phone']}
Subject: {$data['subject']}
Message:
{$data['message']}
";
}
private static function buildErrorReportHtml(string $context, string $errorMessage, $data = []): string
{
if (is_string($data)) {
$decoded = json_decode($data, true);
$data = is_array($decoded) ? $decoded : ['raw_data' => $data];
}
$body = "
Context: {$context}{$errorMessage}
";
if (!empty($data)) {
$body .= "New contact submission received on {$submittedAt}.
{$data['message']}IP: {$data['ip_address']}
User-Agent: {$data['user_agent']}
Hi {$data['first_name']},
Thank you for contacting Wizdom Networks. This message confirms that we received your inquiry on {$submittedAt}.
{$data['message']}We’ll be in touch shortly. If it’s urgent, call us at 416-USE-WISE.
IP: {$data['ip_address']}
User-Agent: {$data['user_agent']}