You are here

public function EmailVerifyManager::checkEmail in Email Verify 8.2

File

src/EmailVerifyManager.php, line 71
Contains \Drupal\email_verify\EmailVerifyManager.

Class

EmailVerifyManager
Defines an email verify manager.

Namespace

Drupal\email_verify

Code

public function checkEmail($email) {

  // Run a quick check to determine if the email appears valid.
  if (!$this->emailValidator
    ->isValid($email)) {
    $this
      ->setError(t('Invalid email: @email.', array(
      '@email' => $email,
    )));
    return;
  }
  $host = Unicode::substr(strstr($email, '@'), 1);
  $this
    ->connect($host);
  $mail_config = $this->configFactory
    ->get('system.site');

  // Get the custom site notification email to use as the from email address
  // if it has been set.
  $site_mail = $mail_config
    ->get('mail_notification');

  // If the custom site notification email has not been set, we use the site
  // default for this.
  if (empty($site_mail)) {
    $site_mail = $mail_config
      ->get('mail');
  }
  if (empty($site_mail)) {
    $site_mail = ini_get('sendmail_from');
  }

  // Extract the <...> part, if there is one.
  if (preg_match('/\\<(.*)\\>/', $from, $match) > 0) {
    $from = $match[1];
  }

  // Should be good enough for RFC compliant SMTP servers.
  $request = $this->requestStack
    ->getCurrentRequest();
  $localhost = $request
    ->getHost();
  if (!$localhost) {
    $localhost = 'localhost';
  }
  fwrite($this->connection, "HELO {$localhost}\r\n");
  fgets($this->connection, 1024);
  fwrite($this->connection, "MAIL FROM: <{$from}>\r\n");
  $from = fgets($this->connection, 1024);
  fwrite($this->connection, "RCPT TO: <{$email}>\r\n");
  $to = fgets($this->connection, 1024);
  fwrite($this->connection, "QUIT\r\n");
  fclose($this->connection);
  if (!preg_match("/^250/", $from)) {

    // Something went wrong before we could really test the address.
    // Be on the safe side and accept it.
    \Drupal::logger('email_verify')
      ->warning('Could not verify email address at host @host: @from', array(
      '@host' => $host,
      '@from' => $from,
    ));
    $this
      ->setError(t('Could not verify email address at host @host: @from.', array(
      '@host' => $host,
      '@from' => $from,
    )));
    return;
  }

  // This server does not like us (noos.fr behaves like this for instance).
  // Any 4xx error also means we couldn't really check except 450, which is
  // explcitely a non-existing mailbox: 450 = "Requested mail action not
  // taken: mailbox unavailable".
  if (preg_match("/(Client host|Helo command) rejected/", $to) || preg_match("/^4/", $to) && !preg_match("/^450/", $to)) {

    // In those cases, accept the email, but log a warning.
    \Drupal::logger('email_verify')
      ->warning('Could not verify email address at host @host: @to', array(
      '@host' => $host,
      '@to' => $to,
    ));
    $this
      ->setError(t('Could not verify email address at host @host: @to.', array(
      '@host' => $host,
      '@to' => $to,
    )));
    return;
  }
  if (!preg_match("/^250/", $to)) {
    \Drupal::logger('email_verify')
      ->warning('Rejected email address: @mail. Reason: @to', array(
      '@mail' => $email,
      '@to' => $to,
    ));
    $this
      ->setError(t('%mail is not a valid email address. Please check the spelling and try again or contact us for clarification.', array(
      '%mail' => "{$email}",
    )));
  }
}