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 Egulias\EmailValidator\Validation\EmailValidation;
15 use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
16 use Symfony\Component\Validator\Constraint;
17 use Symfony\Component\Validator\ConstraintValidator;
18 use Symfony\Component\Validator\Exception\RuntimeException;
19 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
22 * @author Bernhard Schussek <bschussek@gmail.com>
24 class EmailValidator extends ConstraintValidator
31 public function __construct($strict = false)
33 $this->isStrict = $strict;
39 public function validate($value, Constraint $constraint)
41 if (!$constraint instanceof Email) {
42 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Email');
45 if (null === $value || '' === $value) {
49 if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
50 throw new UnexpectedTypeException($value, 'string');
53 $value = (string) $value;
55 if (null === $constraint->strict) {
56 $constraint->strict = $this->isStrict;
59 if ($constraint->strict) {
60 if (!class_exists('\Egulias\EmailValidator\EmailValidator')) {
61 throw new RuntimeException('Strict email validation requires egulias/email-validator ~1.2|~2.0');
64 $strictValidator = new \Egulias\EmailValidator\EmailValidator();
66 if (interface_exists(EmailValidation::class) && !$strictValidator->isValid($value, new NoRFCWarningsValidation())) {
67 $this->context->buildViolation($constraint->message)
68 ->setParameter('{{ value }}', $this->formatValue($value))
69 ->setCode(Email::INVALID_FORMAT_ERROR)
73 } elseif (!interface_exists(EmailValidation::class) && !$strictValidator->isValid($value, false, true)) {
74 $this->context->buildViolation($constraint->message)
75 ->setParameter('{{ value }}', $this->formatValue($value))
76 ->setCode(Email::INVALID_FORMAT_ERROR)
81 } elseif (!preg_match('/^.+\@\S+\.\S+$/', $value)) {
82 $this->context->buildViolation($constraint->message)
83 ->setParameter('{{ value }}', $this->formatValue($value))
84 ->setCode(Email::INVALID_FORMAT_ERROR)
90 $host = (string) substr($value, strrpos($value, '@') + 1);
92 // Check for host DNS resource records
93 if ($constraint->checkMX) {
94 if (!$this->checkMX($host)) {
95 $this->context->buildViolation($constraint->message)
96 ->setParameter('{{ value }}', $this->formatValue($value))
97 ->setCode(Email::MX_CHECK_FAILED_ERROR)
104 if ($constraint->checkHost && !$this->checkHost($host)) {
105 $this->context->buildViolation($constraint->message)
106 ->setParameter('{{ value }}', $this->formatValue($value))
107 ->setCode(Email::HOST_CHECK_FAILED_ERROR)
113 * Check DNS Records for MX type.
115 * @param string $host Host
119 private function checkMX($host)
121 return '' !== $host && checkdnsrr($host, 'MX');
125 * Check if one of MX, A or AAAA DNS RR exists.
127 * @param string $host Host
131 private function checkHost($host)
133 return '' !== $host && ($this->checkMX($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA')));