Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function checkSMTPConnection($email, $password, $host, $port, $secured)
- {
- // Establish a TCP/IP socket connection
- $context = stream_context_create([
- 'ssl' => [
- 'verify_peer' => false,
- 'verify_peer_name' => false,
- ],
- ]);
- $socket = stream_socket_client("tcp://$host:$port", $errno, $errstr, 10, STREAM_CLIENT_CONNECT, $context);
- if ($socket) {
- // Read the server's response
- $response = fread($socket, 8192);
- // Send EHLO/HELO command
- fwrite($socket, "EHLO example.com\r\n");
- $response = fread($socket, 8192);
- // Send STARTTLS command if using secured connection
- if ($secured) {
- fwrite($socket, "STARTTLS\r\n");
- $response = fread($socket, 8192);
- }
- // Authenticate with the server
- fwrite($socket, "AUTH LOGIN\r\n");
- $response = fread($socket, 8192);
- // Send base64-encoded email and password
- fwrite($socket, base64_encode($email) . "\r\n");
- $response = fread($socket, 8192);
- fwrite($socket, base64_encode($password) . "\r\n");
- $response = fread($socket, 8192);
- // Check if authentication is successful
- if (strpos($response, '235') !== false) {
- // SMTP connection successful
- fwrite($socket, "QUIT\r\n");
- fclose($socket);
- return true;
- } else {
- // Authentication failed
- fclose($socket);
- return false;
- }
- } else {
- // SMTP connection failed
- return false;
- }
- }
- // Usage example
- $email = 'your-email@example.com';
- $password = 'your-password';
- $host = 'smtp.example.com';
- $port = 587; // Change this to the appropriate SMTP port
- $secured = true; // Set to true for secured connection (TLS/SSL)
- if (checkSMTPConnection($email, $password, $host, $port, $secured)) {
- echo "SMTP connection successful!";
- } else {
- echo "SMTP connection failed!";
- }
Add Comment
Please, Sign In to add comment