File: /home6/cco26461/public_html/003/test1.php
<?php
// Enable error reporting for debugging purposes (remove in production)
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Initialize variable for default email value
$defaultEmail = "";
// Define a function to send an email with comprehensive error handling
function sendEmail($to, $subject, $message) {
// Validate email format
if (!filter_var($to, FILTER_VALIDATE_EMAIL)) {
error_log("Invalid email address: $to");
return false;
}
// Basic email headers for better compatibility
$headers = "From: webmaster@" . $_SERVER['HTTP_HOST'] . "\r\n";
$headers .= "Reply-To: webmaster@" . $_SERVER['HTTP_HOST'] . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
// Sanitize subject to prevent header injection
$subject = preg_replace("/[\r\n]/", "", $subject);
// Try to send email
if (mail($to, $subject, $message, $headers)) {
return true;
} else {
// Log the error for debugging
error_log("Failed to send email to: $to");
return false;
}
}
// Function to check if mail() function is available
function isMailAvailable() {
return function_exists('mail');
}
?>
<!-- Display a message indicating that upload is working -->
Upload is <b><span style="color: green">WORKING</span></b><br>
<!-- Display mail function status -->
Check Mailing ..<br>
<?php
if (!isMailAvailable()) {
echo '<b><span style="color: red">MAIL FUNCTION NOT AVAILABLE ON THIS SERVER</span></b><br>';
} else {
echo '<b><span style="color: green">MAIL FUNCTION IS AVAILABLE</span></b><br>';
}
?>
<!-- Display server information for debugging -->
<div style="background: #f0f0f0; padding: 10px; margin: 10px 0; font-size: 12px;">
<strong>Server Info:</strong><br>
PHP Version: <?php echo phpversion(); ?><br>
Server: <?php echo $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown'; ?><br>
OS: <?php echo PHP_OS; ?><br>
Mail Function: <?php echo isMailAvailable() ? 'Available' : 'Not Available'; ?>
</div>
<!-- Create a form for users to input email address -->
<form method="post">
<label for="email">Email Address:</label>
<input type="email" name="email" id="email" value="<?php echo htmlspecialchars($defaultEmail); ?>" placeholder="Enter your email" required>
<br><br>
<input type="submit" value="Send Test Email >>">
</form>
<br>
<?php
// Check if the form has been submitted and email field is filled
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['email'])) {
// Sanitize email input
$email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL);
// Validate email
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo '<b style="color: red;">Please enter a valid email address</b>';
} elseif (!isMailAvailable()) {
echo '<b style="color: red;">Mail function is not available on this server. Contact your hosting provider.</b>';
} else {
$xx = mt_rand(); // Generate a random number
// Create message with more details
$message = "EMAIL TEST WORKING!\n\n";
$message .= "Test ID: " . $xx . "\n";
$message .= "Server: " . ($_SERVER['SERVER_SOFTWARE'] ?? 'Unknown') . "\n";
$message .= "PHP Version: " . phpversion() . "\n";
$message .= "Timestamp: " . date('Y-m-d H:i:s') . "\n";
$message .= "This is a test email to verify that the PHP mail system is working correctly.";
$subject = "PHP Mail Test - " . $xx;
// Send email and handle result
$result = sendEmail($email, $subject, $message);
if ($result) {
// Display a success message
echo "<b style='color: green;'>✓ Successfully sent test email to " . htmlspecialchars($email) . " (Test ID: $xx)</b>";
echo "<br><small>Please check your inbox and spam folder for the test email.</small>";
} else {
// Display a failure message
echo "<b style='color: red;'>✗ Failed to send test email to " . htmlspecialchars($email) . "</b>";
// Additional troubleshooting info
echo "<div style='background: #fff0f0; padding: 10px; margin: 10px 0; font-size: 11px;'>";
echo "<strong>Troubleshooting Tips:</strong><br>";
echo "1. Verify the email address is correct<br>";
echo "2. Check your spam folder<br>";
echo "3. Contact your hosting provider to check mail server configuration<br>";
echo "4. Verify PHP mail() function is enabled on the server";
echo "</div>";
}
}
}
?>