/** * Reads the last line from a socket connection * @param resource $conn A reference to connection handler * @param string $line A reference to read line * @return boolean TRUE on success or FALSE on error */ function readLastLineConn(&$conn, &$line) { $result = false; $line = ''; if (!empty($conn) && is_resource($conn) && !feof($conn)) { while (PCPIN_TCP::readLineConn($conn, $line)) { if ($line == '') { break; } elseif (substr($line, 3, 1) == ' ') { $result = true; break; } } } return $result; }
/** * E-Mail address validator * @param string $email E-Mail address * @param int $level Validation level * Value Description * 0 No validation * 1 Well-formness check * 2 Hostname (or DNS record, if Hostname failed) resolution * 3 Recipient account availability check (violates RFC, use with care!) * @return boolean TRUE if email address is valid or FALSE if not */ function checkEmail($email = '', $level = 1) { $valid = false; $email = trim($email); if ($email != '') { $valid = true; if ($level >= 1) { // Well-formness check $valid = (bool) preg_match('/^([a-zA-Z0-9]+[\\._-]?)+[a-zA-Z0-9]+@(((([a-zA-Z0-9]+-?)+[a-zA-Z0-9]+)|([a-zA-Z0-9]{2,}))+\\.)+[a-zA-Z]{2,4}$/', $email); if ($valid && $level >= 2) { // Hostname (or DNS record, if Hostname failed) resolution $hostname = strtolower(substr($email, strpos($email, '@') + 1)); $host = gethostbyname($hostname); if ($host == $hostname) { $host = ''; } if ($host == '') { // Hostname resolutiion failed // Check DNS record $valid = PCPIN_TCP::checkDNS_record($hostname); } else { $valid = true; } if ($valid && $level >= 3) { // Recipient account availability check $valid = false; // Get MX records $ips = PCPIN_TCP::getMXRecords($hostname); if (empty($ips)) { // No MX records found. Using Hostname. $ips = gethostbynamel($hostname); } // Trying to open connection $conn = false; foreach ($ips as $ip) { $conn = null; $errno = null; $errstr = null; if (PCPIN_TCP::connectHost($conn, $errno, $errstr, $ip, 10)) { // Connection opened break; } } $sender_host = !empty($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] != '' ? $_SERVER['HTTP_HOST'] : 'UNKNOWN.HOST'; if (!empty($conn)) { $line = ''; // Gest SMTP server signature if (PCPIN_TCP::readLastLineConn($conn, $line)) { if (220 === PCPIN_TCP::getStatus($line)) { // Send 'HELO' command if (PCPIN_TCP::writeDataConn($conn, "HELO {$sender_host}\r\n")) { // Get an answer if (PCPIN_TCP::readLastLineConn($conn, $line)) { // Check response status if (250 === PCPIN_TCP::getStatus($line)) { // Start email conversation if (PCPIN_TCP::writeDataConn($conn, "MAIL FROM: <test@{$sender_host}>\r\n")) { // Get an answer if (PCPIN_TCP::readLastLineConn($conn, $line)) { // Check response status if (250 === PCPIN_TCP::getStatus($line)) { // Specify recipient mailbox if (PCPIN_TCP::writeDataConn($conn, "RCPT TO: <" . $email . ">\r\n")) { // Get an answer if (PCPIN_TCP::readLastLineConn($conn, $line)) { // Status 250: mailbox exists :) $valid = 250 === PCPIN_TCP::getStatus($line); } } } } } } } } } } } } } } } return $valid; }