4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Constraints;
14 use Symfony\Component\Validator\Context\ExecutionContextInterface;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\ConstraintValidator;
17 use Symfony\Component\Validator\Exception\RuntimeException;
18 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
21 * @author Bernhard Schussek <bschussek@gmail.com>
23 class EmailValidator extends ConstraintValidator
30 public function __construct($strict = false)
32 $this->isStrict = $strict;
38 public function validate($value, Constraint $constraint)
40 if (!$constraint instanceof Email) {
41 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Email');
44 if (null === $value || '' === $value) {
48 if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
49 throw new UnexpectedTypeException($value, 'string');
52 $value = (string) $value;
54 if (null === $constraint->strict) {
55 $constraint->strict = $this->isStrict;
58 if ($constraint->strict) {
59 if (!class_exists('\Egulias\EmailValidator\EmailValidator') || interface_exists('\Egulias\EmailValidator\Validation\EmailValidation')) {
60 throw new RuntimeException('Strict email validation requires egulias/email-validator:~1.2');
63 $strictValidator = new \Egulias\EmailValidator\EmailValidator();
65 if (!$strictValidator->isValid($value, false, true)) {
66 if ($this->context instanceof ExecutionContextInterface) {
67 $this->context->buildViolation($constraint->message)
68 ->setParameter('{{ value }}', $this->formatValue($value))
69 ->setCode(Email::INVALID_FORMAT_ERROR)
72 $this->buildViolation($constraint->message)
73 ->setParameter('{{ value }}', $this->formatValue($value))
74 ->setCode(Email::INVALID_FORMAT_ERROR)
80 } elseif (!preg_match('/^.+\@\S+\.\S+$/', $value)) {
81 if ($this->context instanceof ExecutionContextInterface) {
82 $this->context->buildViolation($constraint->message)
83 ->setParameter('{{ value }}', $this->formatValue($value))
84 ->setCode(Email::INVALID_FORMAT_ERROR)
87 $this->buildViolation($constraint->message)
88 ->setParameter('{{ value }}', $this->formatValue($value))
89 ->setCode(Email::INVALID_FORMAT_ERROR)
96 $host = (string) substr($value, strrpos($value, '@') + 1);
98 // Check for host DNS resource records
99 if ($constraint->checkMX) {
100 if (!$this->checkMX($host)) {
101 if ($this->context instanceof ExecutionContextInterface) {
102 $this->context->buildViolation($constraint->message)
103 ->setParameter('{{ value }}', $this->formatValue($value))
104 ->setCode(Email::MX_CHECK_FAILED_ERROR)
107 $this->buildViolation($constraint->message)
108 ->setParameter('{{ value }}', $this->formatValue($value))
109 ->setCode(Email::MX_CHECK_FAILED_ERROR)
117 if ($constraint->checkHost && !$this->checkHost($host)) {
118 if ($this->context instanceof ExecutionContextInterface) {
119 $this->context->buildViolation($constraint->message)
120 ->setParameter('{{ value }}', $this->formatValue($value))
121 ->setCode(Email::HOST_CHECK_FAILED_ERROR)
124 $this->buildViolation($constraint->message)
125 ->setParameter('{{ value }}', $this->formatValue($value))
126 ->setCode(Email::HOST_CHECK_FAILED_ERROR)
133 * Check DNS Records for MX type.
135 * @param string $host Host
139 private function checkMX($host)
141 return '' !== $host && checkdnsrr($host, 'MX');
145 * Check if one of MX, A or AAAA DNS RR exists.
147 * @param string $host Host
151 private function checkHost($host)
153 return '' !== $host && ($this->checkMX($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA')));