Comments in email addresses

Quick links: Source codeEmail address validation service

[UPDATE 15 March 2011: see my comment below]

I was turning a blind eye to the part of RFC5322 that allows you to put comments within an email address. But Cal H brought it up in an email so I had to bite the bullet.

On reflection I think this was worthwhile. The most common error in email address validators is that they reject valid addresses. This really annoys people who like to put a ‘+’ in their address and find they can’t because registration form won’t allow it.

Why do they like putting a ‘+’ in their address? Well it effectively tags the incoming email for you automatically. Mail sent to first.last+hello@example.com will go to the first.last mailbox, tagged with ‘hello’. GMail will do this for you – try it.

So that’s why I think it’s worth allowing comments. The next GMail might be able to do the same thing or something even more useful with comments:

first.last(notify IM)@example.com

Version 1.6 of my validator now passes all 222 unit tests. So does Cal’s. I see no reason why you wouldn’t use one of these in your project: they are free and they work. Why reinvent the wheel?

RFC nerd notes

Comments can contain folding white space and can be nested. This is the final nail in the coffin for regular expressions that claim to validate email address. Show me a regex that says this is a valid address:

first(Welcome to
the (“wonderful” (!)) world
of email)@example.com

A thank-you also to Paul Gregg who allowed me to add his validator to the head-to-head (and added mine to his page). He also provided some more unit tests.

Quick links: Source code | Email address validation service


4 thoughts on “Comments in email addresses

  1. On further reflection, this was the biggest mistake I made.

    Comments are not allowed in an RFC 5321 Mailbox. This is by far the most common use-case for email address validation.

    RFC 5322 addr-spec compliance is only relevant for the To: or Cc: field of an email message. NOT for the envelope.

    Unless you are writing an MTA or webmail application you will not need anything except RFC 5321 compliance.

  2. “Show me a regex that says this is a valid address:”

    i’ll show you two of them!

    permissive: /^(.|n)+@.+$/
    strict: /^first(Welcome tonthe (?wonderful? (!)) worldnof email)@example.com$/

    won’t be surprised if the comment system fubars them.

  3. (I realize this is an old post…)

    I like to use the + in my google email address, but is this a google only convention or is there an rfc that says anything after the + is a comment?

    I think a dot can be inserted into the local part of any google (or is it yahoo) email address. I believe the email address is treated as if the dot does not exist.

    -Kelly

    • There’s nothing in the RFCs to say that + has any semantic meaning, or that dots are insignificant. Both those are purely Gmail policy decisions.

Leave a Reply