Test_smtp.php

PHP Detected Guest 51 Views Size: 2.87 KB Posted on: Oct 24, 25 @ 12:39 PM
  1. <?php
  2. // test_smtp.php - Standalone test script to verify PHPMailer SMTP functionality
  3. // Assumes database is set up with 'mail' and 'site_info' tables containing necessary settings.
  4. require_once __DIR__ . '/../config.php';
  5. require_once __DIR__ . '/mail.php';
  6.  
  7. // Start session if not already started
  8. if (session_status() === PHP_SESSION_NONE) {
  9.         'cookie_secure' => isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on',
  10.         'cookie_httponly' => true,
  11.         'use_strict_mode' => true,
  12.     ]);
  13. }
  14.  
  15. // Generate CSRF token if not set
  16. if (empty($_SESSION['csrf_token'])) {
  17.     $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
  18.     error_log("test_smtp.php: Generated new CSRF token");
  19. }
  20.  
  21. error_log("test_smtp.php: Script started");
  22.  
  23. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  24.     error_log("test_smtp.php: POST request received");
  25.    
  26.     // Retrieve form inputs
  27.     $to = trim($_POST['to'] ?? '');
  28.     $csrf_token = $_POST['csrf_token'] ?? '';
  29.    
  30.     error_log("test_smtp.php: Recipient: $to, CSRF Token: $csrf_token");
  31.    
  32.     // Test email details
  33.     $subject = 'Test SMTP Email from PHPMailer';
  34.     $message = '<p>This is a test email sent via PHPMailer</p><p>If you received this, SMTP is working!</p>';
  35.     $name = 'Test User';
  36.    
  37.     // Call the send_mail function from mail.php
  38.     error_log("test_smtp.php: Calling send_mail function");
  39.     $result = send_mail($to, $subject, $message, $name, $csrf_token);
  40.    
  41.     error_log("test_smtp.php: send_mail result - Status: " . $result['status'] . ", Message: " . $result['message']);
  42.    
  43.     // Output the result
  44.     if ($result['status'] === 'success') {
  45.         echo '<p style="color: green;">' . htmlspecialchars($result['message']) . '</p>';
  46.     } else {
  47.         echo '<p style="color: red;">' . htmlspecialchars($result['message']) . '</p>';
  48.     }
  49.    
  50.     echo '<p><a href="">Send another test</a></p>';
  51. } else {
  52.     error_log("test_smtp.php: GET request - Displaying form");
  53.     // Display the form
  54.     ?>
  55.     <!DOCTYPE html>
  56.     <html lang="en">
  57.     <head>
  58.         <meta charset="UTF-8">
  59.         <title>Test SMTP with PHPMailer</title>
  60.     </head>
  61.     <body>
  62.         <h1>Test SMTP</h1>
  63.         <p>This script uses your configured mail settings from the database to send a test email via PHPMailer.</p>
  64.         <p>Ensure your mail settings (SMTP host, port, auth, etc.) are set in the admin panel, and database is configured.</p>
  65.         <form method="post">
  66.             <label for="to">Recipient Email:</label><br>
  67.             <input type="email" id="to" name="to" required><br><br>
  68.             <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($_SESSION['csrf_token']); ?>">
  69.             <button type="submit">Send Test Email</button>
  70.         </form>
  71.     </body>
  72.     </html>
  73.     <?php
  74. }
  75.  
  76. error_log("test_smtp.php: Script ended");
  77. ?>

Raw Paste

Comments 0
Login to post a comment.
  • No comments yet. Be the first.
Login to post a comment. Login or Register
We use cookies. To comply with GDPR in the EU and the UK we have to show you these.

We use cookies and similar technologies to keep this website functional (including spam protection via Google reCAPTCHA or Cloudflare Turnstile), and — with your consent — to measure usage and show ads. See Privacy.