isSMTP(); $mail->Host = $smtp_host; $mail->SMTPAuth = true; $mail->Username = $smtp_username; $mail->Password = $smtp_password; self::configureSmtpEncryption($mail, $smtp_encryption); $mail->Port = $smtp_port; // Recipients $mail->setFrom($from_email, $from_name); if (is_array($to)) { foreach ($to as $recipient) { $mail->addAddress($recipient); } } else { $mail->addAddress($to); } // Process headers foreach ($headers as $header) { if (stripos($header, 'content-type:') === 0) { if (stripos($header, 'text/html') !== false) { $mail->isHTML(true); } } elseif (stripos($header, 'cc:') === 0) { $cc = trim(substr($header, 3)); $mail->addCC($cc); } elseif (stripos($header, 'bcc:') === 0) { $bcc = trim(substr($header, 4)); $mail->addBCC($bcc); } elseif (stripos($header, 'reply-to:') === 0) { $reply_to = trim(substr($header, 9)); $mail->addReplyTo($reply_to); } } // Content $mail->Subject = $subject; $mail->Body = $message; // Attachments foreach ($attachments as $attachment) { $mail->addAttachment($attachment); } $mail->send(); return true; } catch (Exception $e) { return false; } } private static function configureSmtpEncryption(PHPMailer $mail, string $smtp_encryption): void { if ($smtp_encryption === 'ssl') { $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; $mail->SMTPAutoTLS = true; return; } if ($smtp_encryption === 'tls') { $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $mail->SMTPAutoTLS = true; return; } $mail->SMTPSecure = ''; $mail->SMTPAutoTLS = false; } /** * Custom from email filter */ public static function customFromEmail($email) { $from_email = SettingsService::getString('from_email', ''); return !empty($from_email) ? $from_email : $email; } /** * Custom from name filter */ public static function customFromName($name) { $from_name = SettingsService::getString('from_name', ''); return !empty($from_name) ? $from_name : $name; } /** * Test SMTP connection * * @return array Result with success status and message */ public static function testSMTPConnection(): array { $smtp_host = SettingsService::getString('smtp_host', ''); $smtp_port = SettingsService::getInt('smtp_port', 587); $smtp_username = SettingsService::getString('smtp_username', ''); $smtp_password = SettingsService::getString('smtp_password', ''); $smtp_encryption = SettingsService::getString('smtp_encryption', 'tls'); if (empty($smtp_host) || empty($smtp_username) || empty($smtp_password)) { return [ 'success' => false, 'message' => __('Please configure all SMTP settings first.', 'yatra') ]; } try { $mail = new PHPMailer(true); $mail->isSMTP(); $mail->Host = $smtp_host; $mail->SMTPAuth = true; $mail->Username = $smtp_username; $mail->Password = $smtp_password; self::configureSmtpEncryption($mail, $smtp_encryption); $mail->Port = $smtp_port; $mail->Timeout = 10; // Test connection $mail->smtpConnect(); $mail->smtpClose(); return [ 'success' => true, 'message' => __('SMTP connection successful!', 'yatra') ]; } catch (Exception $e) { return [ 'success' => false, 'message' => sprintf( /* translators: %s: SMTP error message. */ __('SMTP connection failed: %s', 'yatra'), $e->getMessage() ) ]; } } }