Search code examples
phpsymfonyswiftmailersymfony-validator

Email compliance and validation with RFC 2822, 3.6.2 in Symfony


I have an issue while validating a user email address through the Symfony Email Constraint and then trying to send an mail to this user through Swiftmailer.

Let's say my user is registering with name@@gmail.com (the double @ is an intentional typo for the example). This is successfully validated by the Email Constraint validation in Symfony, and the user is created in my database.

But when I try to send the mail using Swiftmailer, I get a 500 error

Address in mailbox given [name@@gmail.com] does not comply with RFC 2822, 3.6.2.

coming from Swiftmailer itself.


Solution

  • You can enable the strict mode in Symfony's EmailValidator, e.g. like this:

    class Author
    {
        /**
         * @Assert\Email(
         *     message = "The email '{{ value }}' is not a valid email.",
         *     mode = "strict"
         * )
         */
        protected $email;
    }
    

    As the docs say:

    Uses the egulias/email-validator library to perform an RFC compliant validation. You will need to install that library to use this mode.

    The egulias/email-validator is also used by SwiftMailer (see Swiftmailer's composer.json and PathHeader), so this should give you similar validation results.