public static function PHPMailer::isValidHost

5.x PHPMailer.php public static PHPMailer::isValidHost($host)

Validate whether a string contains a valid value to use as a hostname or IP address. IPv6 addresses must include [], e.g. `[::1]`, not just `::1`.

Parameters

string $host The host name or IP address to check:

Return value

bool

2 calls to PHPMailer::isValidHost()
PHPMailer::serverHostname in inc/PHPMailer/src/PHPMailer.php
Get the server hostname. Returns 'localhost.localdomain' if unknown.
PHPMailer::smtpConnect in inc/PHPMailer/src/PHPMailer.php
Initiate a connection to an SMTP server. Returns false if the operation failed.

File

inc/PHPMailer/src/PHPMailer.php, line 3753

Class

PHPMailer
PHPMailer - PHP email creation and transport class.

Namespace

PHPMailer\PHPMailer

Code

public static function isValidHost($host) 
 {
  //Simple syntax limits
  if (empty($host)
   or !is_string($host)
     or strlen($host) > 256
    ) {
    return false;
  }
  //Looks like a bracketed IPv6 address
  if (trim($host, '[]') != $host) {
    return (bool) filter_var(trim($host, '[]'), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
  }
  //If removing all the dots results in a numeric string, it must be an IPv4 address.
  //Need to check this first because otherwise things like `999.0.0.0` are considered valid host names
  if (is_numeric(str_replace('.', '', $host))) {
    //Is it a valid IPv4 address?
    return (bool) filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
  }
  if (filter_var('http://' . $host, FILTER_VALIDATE_URL)) {
    //Is it a syntactically valid hostname?
    return true;
  }

  return false;
}