When setting up a mail server (like Postfix + Dovecot) or debugging email delivery, it’s often necessary to verify whether your SMTP host works correctly and if your credentials are valid.
Here are two practical methods you can use with PHP 7.4.
✅ Method 1: Using PHPMailer (Recommended)
PHPMailer is a full-featured mailer library supporting STARTTLS / SMTPS / AUTH LOGIN and detailed debugging logs.
Install via Composer
composer require phpmailer/phpmailer:^6.8
Example: smtp_test.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require __DIR__ . '/vendor/autoload.php';
$mail = new PHPMailer(true);
$host = 'smtp.example.com';
$port = 587;
$username = 'user@example.com';
$password = 'your_password';
$from = 'user@example.com';
$to = 'you@example.com';
try {
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = $host;
$mail->SMTPAuth = true;
$mail->Username = $username;
$mail->Password = $password;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = $port;
$mail->setFrom($from, 'SMTP Tester');
$mail->addAddress($to);
$mail->Subject = 'SMTP Test Email';
$mail->Body = 'This is a test message sent using PHPMailer.';
$mail->send();
echo "✅ Email sent successfully.\n";
} catch (Exception $e) {
echo "❌ Mail send failed: {$mail->ErrorInfo}\n";
}
If you don’t use Composer, manually include
PHPMailer.php,SMTP.php, andException.phpfrom the official PHPMailer repository.
✅ Method 2: Pure PHP Socket Test (No Email Sent)
This method only checks connection → STARTTLS → AUTH LOGIN, without sending an actual message.
It’s perfect for quickly validating SMTP connectivity and authentication.
<?php
$host = 'smtp.example.com';
$port = 587;
$user = 'user@example.com';
$pass = 'your_password';
function expect($fp, $prefix, $desc) {
$line = '';
while ($row = fgets($fp)) {
$line .= $row;
if (strlen($row) >= 4 && $row[3] === ' ') break;
}
if (strpos($line, $prefix) !== 0) {
throw new RuntimeException("Expected {$desc} reply {$prefix}, got:\n{$line}");
}
echo "✔ {$desc}:\n{$line}\n";
return $line;
}
$fp = stream_socket_client("tcp://{$host}:{$port}", $errno, $errstr, 10);
expect($fp, '220', 'Greeting');
fwrite($fp, "EHLO test.local\r\n");
expect($fp, '250', 'EHLO');
fwrite($fp, "STARTTLS\r\n");
expect($fp, '220', 'STARTTLS ready');
stream_socket_enable_crypto($fp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
fwrite($fp, "EHLO test.local\r\n");
expect($fp, '250', 'EHLO after TLS');
fwrite($fp, "AUTH LOGIN\r\n");
expect($fp, '334', 'AUTH started');
fwrite($fp, base64_encode($user) . "\r\n");
expect($fp, '334', 'Username accepted');
fwrite($fp, base64_encode($pass) . "\r\n");
expect($fp, '235', 'Authentication successful');
fwrite($fp, "QUIT\r\n");
fclose($fp);
echo "✅ Connection + STARTTLS + AUTH successful.\n";
🔍 Common Issues
| Error | Cause & Fix |
|---|---|
Connection timed out | Wrong hostname or blocked port |
Peer certificate CN mismatch | Certificate’s CN/SAN doesn’t match host |
535 Authentication failed | Wrong username/password or incomplete login format |
STARTTLS not supported | You’re using port 465 (SMTPS), which already uses TLS |
🧩 Conclusion
If you need to send test emails, use PHPMailer.
If you want to debug TLS or AUTH handshake, use the pure PHP socket script.
Both methods let you quickly verify if your SMTP server and credentials are working correctly.