File: /home/mytest/.trash/test-contact-email.php.71
<?php
/**
* Contact Form Email Debug Script
* Tests SMTP email sending with detailed logging
*/
require_once __DIR__ . '/database/Database.php';
require_once __DIR__ . '/config.php';
$logFile = __DIR__ . '/contact-email-debug.log';
function logDebug($message) {
global $logFile;
$timestamp = date('Y-m-d H:i:s');
file_put_contents($logFile, "[$timestamp] $message\n", FILE_APPEND);
echo "[$timestamp] $message\n";
}
function sendSMTPEmailDebug($host, $port, $username, $password, $fromEmail, $fromName, $toEmail, $subject, $message, $replyTo = '') {
logDebug("=== Starting SMTP Email Test ===");
logDebug("Host: $host");
logDebug("Port: $port");
logDebug("Username: $username");
logDebug("From: $fromName <$fromEmail>");
logDebug("To: $toEmail");
logDebug("Reply-To: $replyTo");
logDebug("Subject: $subject");
try {
logDebug("Attempting to connect to ssl://$host:$port");
$socket = fsockopen('ssl://' . $host, $port, $errno, $errstr, 30);
if (!$socket) {
logDebug("ERROR: Connection failed - $errstr ($errno)");
return false;
}
logDebug("SUCCESS: Connected to SMTP server");
$response = fgets($socket, 515);
logDebug("Server greeting: " . trim($response));
if (substr($response, 0, 3) != '220') {
logDebug("ERROR: Invalid server greeting");
fclose($socket);
return false;
}
fputs($socket, "EHLO " . $host . "\r\n");
$response = fgets($socket, 515);
logDebug("EHLO response: " . trim($response));
fputs($socket, "AUTH LOGIN\r\n");
$response = fgets($socket, 515);
logDebug("AUTH LOGIN response: " . trim($response));
fputs($socket, base64_encode($username) . "\r\n");
$response = fgets($socket, 515);
logDebug("Username response: " . trim($response));
fputs($socket, base64_encode($password) . "\r\n");
$response = fgets($socket, 515);
logDebug("Password response: " . trim($response));
if (substr($response, 0, 3) != '235') {
logDebug("ERROR: Authentication failed");
fclose($socket);
return false;
}
logDebug("SUCCESS: Authenticated");
fputs($socket, "MAIL FROM: <" . $fromEmail . ">\r\n");
$response = fgets($socket, 515);
logDebug("MAIL FROM response: " . trim($response));
fputs($socket, "RCPT TO: <" . $toEmail . ">\r\n");
$response = fgets($socket, 515);
logDebug("RCPT TO response: " . trim($response));
fputs($socket, "DATA\r\n");
$response = fgets($socket, 515);
logDebug("DATA response: " . trim($response));
$date = date('r');
$headers = "Subject: " . $subject . "\r\n";
$headers .= "To: " . $toEmail . "\r\n";
$headers .= "From: " . $fromName . " <" . $fromEmail . ">\r\n";
$headers .= "Reply-To: " . ($replyTo ?: $fromEmail) . "\r\n";
$headers .= "Date: " . $date . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .= "X-Mailer: YOLO Charters CMS\r\n";
$emailContent = $headers . "\r\n";
$emailContent .= $message . "\r\n";
$emailContent .= ".\r\n";
fputs($socket, $emailContent);
$response = fgets($socket, 515);
logDebug("Send response: " . trim($response));
fputs($socket, "QUIT\r\n");
fclose($socket);
$success = substr($response, 0, 3) == '250';
logDebug($success ? "SUCCESS: Email sent!" : "ERROR: Email not sent");
logDebug("=== End SMTP Email Test ===\n");
return $success;
} catch (Exception $e) {
logDebug("EXCEPTION: " . $e->getMessage());
logDebug("=== End SMTP Email Test ===\n");
return false;
}
}
if (file_exists($logFile)) {
unlink($logFile);
}
logDebug("Contact Form Email Debug Script");
logDebug("================================\n");
$db = Database::getInstance()->getConnection();
$smtpSettings = [];
$settingKeys = ['smtp_host', 'smtp_port', 'smtp_username', 'smtp_password', 'smtp_from_email', 'smtp_from_name', 'company_email'];
foreach ($settingKeys as $key) {
$stmt = $db->prepare("SELECT setting_value FROM settings WHERE setting_key = ?");
$stmt->bindValue(1, $key, SQLITE3_TEXT);
$result = $stmt->execute();
$row = $result->fetchArray(SQLITE3_ASSOC);
$smtpSettings[$key] = $row ? $row['setting_value'] : '';
}
logDebug("SMTP Settings Retrieved:");
logDebug("Host: " . ($smtpSettings['smtp_host'] ?: 'NOT SET'));
logDebug("Port: " . ($smtpSettings['smtp_port'] ?: 'NOT SET'));
logDebug("Username: " . ($smtpSettings['smtp_username'] ?: 'NOT SET'));
logDebug("Password: " . (empty($smtpSettings['smtp_password']) ? 'NOT SET' : '***SET***'));
logDebug("From Email: " . ($smtpSettings['smtp_from_email'] ?: 'NOT SET'));
logDebug("From Name: " . ($smtpSettings['smtp_from_name'] ?: 'NOT SET'));
logDebug("Company Email: " . ($smtpSettings['company_email'] ?: 'NOT SET'));
logDebug("");
if (empty($smtpSettings['smtp_host']) || empty($smtpSettings['smtp_username']) || empty($smtpSettings['smtp_password'])) {
logDebug("ERROR: SMTP settings are not configured properly!");
logDebug("Please configure SMTP settings in the admin panel.");
exit(1);
}
$toEmail = $smtpSettings['company_email'] ?: 'info@yolo-charters.com';
$subject = 'Test Email - YOLO Charters Contact Form';
$message = "This is a test email from the YOLO Charters contact form debug script.\n\n";
$message .= "If you receive this email, the SMTP configuration is working correctly.\n\n";
$message .= "Test Details:\n";
$message .= "- Date: " . date('Y-m-d H:i:s') . "\n";
$message .= "- SMTP Host: " . $smtpSettings['smtp_host'] . "\n";
$message .= "- SMTP Port: " . $smtpSettings['smtp_port'] . "\n";
$result = sendSMTPEmailDebug(
$smtpSettings['smtp_host'],
$smtpSettings['smtp_port'],
$smtpSettings['smtp_username'],
$smtpSettings['smtp_password'],
$smtpSettings['smtp_from_email'],
$smtpSettings['smtp_from_name'],
$toEmail,
$subject,
$message,
$smtpSettings['smtp_from_email']
);
if ($result) {
logDebug("\n✓ Email sent successfully!");
logDebug("Check your inbox at: $toEmail");
} else {
logDebug("\n✗ Email failed to send!");
logDebug("Check the log above for details.");
}
logDebug("\nLog file saved to: $logFile");
?>