public static function PHPMailer::validateAddress

5.x PHPMailer.php public static PHPMailer::validateAddress($address, $patternselect = null)

Check that a string looks like an email address. Validation patterns supported:

  • `auto` Pick best pattern automatically;
  • `pcre8` Use the squiloople.com pattern, requires PCRE > 8.0;
  • `pcre` Use old PCRE implementation;
  • `php` Use PHP built-in FILTER_VALIDATE_EMAIL;
  • `html5` Use the pattern given by the HTML5 spec for 'email' type form input elements.
  • `noregex` Don't use a regex: super fast, really dumb.

Alternatively you may pass in a callable to inject your own validator, for example:

```php PHPMailer::validateAddress('user@example.com', function($address) { return (strpos($address, '@') !== false); }); ```

You can also set the PHPMailer::$validator static to a callable, allowing built-in methods to use your validator.

Parameters

string $address The email address to check:

string|callable $patternselect Which pattern to use:

Return value

bool

4 calls to PHPMailer::validateAddress()
PHPMailer::addAnAddress in inc/PHPMailer/src/PHPMailer.php
Add an address to one of the recipient arrays or to the ReplyTo array. Addresses that have been added already return false, but do not throw exceptions.
PHPMailer::mailSend in inc/PHPMailer/src/PHPMailer.php
Send mail using the PHP mail() function.
PHPMailer::parseAddresses in inc/PHPMailer/src/PHPMailer.php
Parse and validate a string containing one or more RFC822-style comma-separated email addresses of the form "display name <address>" into an array of name/address pairs. Uses the imap_rfc822_parse_adrlist function if the IMAP extension…
PHPMailer::setFrom in inc/PHPMailer/src/PHPMailer.php
Set the From and FromName properties.

File

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

Class

PHPMailer
PHPMailer - PHP email creation and transport class.

Namespace

PHPMailer\PHPMailer

Code

public static function validateAddress($address, $patternselect = null) 
 {
  if (null === $patternselect) {
    $patternselect = static::$validator;
  }
  if (is_callable($patternselect)) {
    return call_user_func($patternselect, $address);
  }
  //Reject line breaks in addresses; it's valid RFC5322, but not RFC5321
  if (strpos($address, "\n") !== false or strpos($address, "\r") !== false) {
    return false;
  }
  switch ($patternselect) {
    case 'pcre': //Kept for BC
    case 'pcre8':
      /*
       * A more complex and more permissive version of the RFC5322 regex on which FILTER_VALIDATE_EMAIL
       * is based.
       * In addition to the addresses allowed by filter_var, also permits:
       *  * dotless domains: `a@b`
       *  * comments: `1234 @ local(blah) .machine .example`
       *  * quoted elements: `'"test blah"@example.org'`
       *  * numeric TLDs: `a@b.123`
       *  * unbracketed IPv4 literals: `a@192.168.0.1`
       *  * IPv6 literals: 'first.last@[IPv6:a1::]'
       * Not all of these will necessarily work for sending!
       *
       * @see       http://squiloople.com/2009/12/20/email-address-validation/
       * @copyright 2009-2010 Michael Rushton
       * Feel free to use and redistribute this code. But please keep this copyright notice.
       */
      return (bool) preg_match(
      '/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)' .
        '((?>(?>(?>((?>(?>(?>\x0D\x0A)?[\t ])+|(?>[\t ]*\x0D\x0A)?[\t ]+)?)(\((?>(?2)' .
        '(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)' .
        '([!#-\'*+\/-9=?^-~-]+|"(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\x7F]))*' .
        '(?2)")(?>(?1)\.(?1)(?4))*(?1)@(?!(?1)[a-z0-9-]{64,})(?1)(?>([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)' .
        '(?>(?1)\.(?!(?1)[a-z0-9-]{64,})(?1)(?5)){0,126}|\[(?:(?>IPv6:(?>([a-f0-9]{1,4})(?>:(?6)){7}' .
        '|(?!(?:.*[a-f0-9][:\]]){8,})((?6)(?>:(?6)){0,6})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:' .
        '|(?!(?:.*[a-f0-9]:){6,})(?8)?::(?>((?6)(?>:(?6)){0,4}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}' .
        '|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD', 
      $address
      );
    case 'html5':
      /*
       * This is the pattern used in the HTML5 spec for validation of 'email' type form input elements.
       *
       * @see http://www.whatwg.org/specs/web-apps/current-work/#e-mail-state-(type=email)
       */
      return (bool) preg_match(
      '/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}' .
        '[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/sD', 
      $address
      );
    case 'php':
    default:
      return (bool) filter_var($address, FILTER_VALIDATE_EMAIL);
  }
}