Feb 28, 2012

Validating e-mail

Just the code:

protected function validateEmail() {
    $email = $this->getPostParameter('email');
    if (!t3lib_div::validEmail($email)) {
        $this->setError('bad_email');
        return false;
    }

    $parts = explode('@', $email);

    $result = array(); $returnCode = 0;
    exec('nslookup -q=mx ' . escapeshellarg($parts[1]) . ' 8.8.8.8', $result, $returnCode);
    if ($returnCode != 127) {
        if (strpos(implode('', $result), 'mail exchanger') === false) {
            $this->setError('bad_email');
            return false;
        }
    }
    return true;
}

2 comments:

  1. You might use http://php.net/manual/en/function.getmxrr.php for that. It has safe fallback to always-true on Windows system.

    ReplyDelete
  2. Will not work on systems with exec calls deactivated.

    ReplyDelete