Your IP : 216.73.216.40


Current Path : /home/ladengineer/public_html/wp-includes/images/smilies/lib/
Upload File :
Current File : /home/ladengineer/public_html/wp-includes/images/smilies/lib/smtp_test.php

<?php
// smtp_test.php - Clean & Robust SMTP Test

header('Content-Type: application/json');
error_reporting(0);        // Hide PHP warnings/notices from output
ini_set('display_errors', 0);

$input = json_decode(file_get_contents('php://input'), true);

if (!$input) {
    echo json_encode(['success' => false, 'message' => 'Invalid JSON input']);
    exit;
}

// Required fields
$required = ['host', 'port', 'user', 'pass', 'senderName', 'senderEmail', 'recipientEmail'];
foreach ($required as $field) {
    if (empty($input[$field])) {
        echo json_encode(['success' => false, 'message' => "Missing field: $field"]);
        exit;
    }
}

$host          = trim($input['host']);
$port          = (int)$input['port'];
$user          = trim($input['user']);
$pass          = $input['pass'];
$encrypt       = strtolower(trim($input['encrypt'] ?? ''));
$senderName    = trim($input['senderName']);
$senderEmail   = trim($input['senderEmail']);
$recipientEmail= trim($input['recipientEmail']);

// Basic validation
if ($port < 1 || $port > 65535) {
    echo json_encode(['success' => false, 'message' => 'Invalid port number']);
    exit;
}

if (!filter_var($senderEmail, FILTER_VALIDATE_EMAIL) || !filter_var($recipientEmail, FILTER_VALIDATE_EMAIL)) {
    echo json_encode(['success' => false, 'message' => 'Invalid email address format']);
    exit;
}

// Load PHPMailer
require_once __DIR__ . '/PHPMailer.php';
require_once __DIR__ . '/SMTP.php';
require_once __DIR__ . '/Exception.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);

try {
    $mail->isSMTP();
    $mail->Host       = $host;
    $mail->SMTPAuth   = true;
    $mail->Username   = $user;
    $mail->Password   = $pass;
    $mail->Port       = $port;
    $mail->CharSet    = 'UTF-8';

    // Encryption settings
    if ($encrypt === 'ssl') {
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
    } elseif ($encrypt === 'tls') {
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    } else {
        $mail->SMTPSecure = '';
        $mail->SMTPAutoTLS = false;
    }

    // Optional: Uncomment if you have SSL certificate issues
    // $mail->SMTPOptions = [
    //     'ssl' => [
    //         'verify_peer' => false,
    //         'verify_peer_name' => false,
    //         'allow_self_signed' => true
    //     ]
    // ];

    $mail->setFrom($senderEmail, $senderName);
    $mail->addAddress($recipientEmail);
    $mail->Subject = 'Leaf Mailer - SMTP Test Successful';
    $mail->Body    = "Hello,\n\nThis is a test email sent from Leaf Mailer.\n\nIf you received this, your SMTP settings are working correctly!\n\nBest regards,\nLeaf Mailer";

    $mail->send();

    echo json_encode([
        'success' => true,
        'message' => 'SMTP test successful! Test email has been sent.'
    ]);

} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => 'SMTP Error: ' . $mail->ErrorInfo
    ]);
} catch (Throwable $e) {
    echo json_encode([
        'success' => false,
        'message' => 'Server Error: ' . $e->getMessage()
    ]);
}