如何使用 SMTP 握手验证电子邮件地址有效/无效?我的代码总是显示无效电子邮件不存在。但实际上它是存在的。
在此代码中,我使用 SMTP 详细信息来验证收件人电子邮件是否有效。
$smtpHost = 'mail.sender.com'; // Replace with your SMTP server hostname
$smtpPort = 587; // Use the appropriate port for your SMTP server
$localDomain = 'sender.com'; // Replace with the name of your server
$recipientEmail = '[email protected]'; // Replace with the recipient's email
$username = '[email protected]';
$password = '**********';
list(,$domain) = explode("@",$recipientEmail);
// Open a socket connection to the SMTP server
$socket = fsockopen($smtpHost, $smtpPort, $errno, $errstr, 30);
if (!$socket) {
echo "Failed to open socket: $errstr ($errno)";
} else {
function readResponse($socket) {
$response = '';
do {
$line = fgets($socket, 512);
$response .= $line;
} while (preg_match('/^\d{3}-/', $line));
echo $response;
return $response;
}
// Read the initial response from the server
$response = readResponse($socket);
// Send the EHLO (or HELO) command to the SMTP server
fputs($socket, "EHLO $localDomain\r\n"); // Use EHLO or HELO as needed
$response = readResponse($socket);
// Check if the server responds with a 250 code indicating a successful greeting
if (preg_match('/^250/', $response)) {
// Send the AUTH command with your SMTP credentials (username and password)
$response = readResponse($socket);
fputs($socket, "AUTH LOGIN\r\n");
$response = readResponse($socket);
if (preg_match('/^334/', $response)) {
// Send the base64-encoded username
fputs($socket, base64_encode($username) . "\r\n");
$response = readResponse($socket);
if (preg_match('/^334/', $response)) {
// Send the base64-encoded password
fputs($socket, base64_encode($password) . "\r\n");
$response = readResponse($socket);
// Check if authentication was successful (usually 235 for success)
if (preg_match('/^235/', $response)) {
// Send the sender's email address
fputs($socket, "MAIL FROM:<$username>\r\n");
$response = readResponse($socket);
// Now you can send the RCPT TO command
fputs($socket, "RCPT TO:<$recipientEmail>\r\n");
$response = readResponse($socket);
// Send VRFY command
fputs($socket, "VRFY $recipientEmail\r\n");
$response = readResponse($socket);
// Check the response
if (preg_match('/^250/', $response)) {
echo "The email address '$recipientEmail' exists on the server.\r\n";
} else {
echo "The email address '$recipientEmail' does not exist or the server doesn't support VRFY.\r\n";
}
// Close the connection
fputs($socket, "QUIT\r\n");
$response = readResponse($socket);
} else {
echo "SMTP authentication failed.\r\n";
}
} else {
echo "Failed to send the password.\r\n";
}
} else {
echo "Failed to send the username.\r\n";
}
} else {
echo "Failed to greet the SMTP server.\r\n";
}
// Close the socket connection when done
fclose($socket);
}
我想使用 PHP Socket SMTP Handshake 检查电子邮件地址是否有效/无效
VRFY 不是验证电子邮件有效/无效的好选择。我解决了这个问题,添加了对 RCPT TO 的检查。请检查以下代码
// Now you can send the RCPT TO command
fputs($socket, "RCPT TO:<$recipientEmail>\r\n");
$response = readResponse($socket);
// Check the response
if (preg_match('/^250/', $response)) {
echo "The email address '$recipientEmail' is accepted by the server.\r\n";
} else {
echo "The email address '$recipientEmail' is not accepted or there was an error.\r\n";
}