开发者

Is it possible to include comments inside a non email host name?

开发者 https://www.devze.com 2023-02-07 03:41 出处:网络
I am working on a more complete email validator in java and came across an interesting ability to embed comments within an email both in the \"username\" and \"address\" portions.

I am working on a more complete email validator in java and came across an interesting ability to embed comments within an email both in the "username" and "address" portions.

The following snippet from http://www.dominicsayers.com/isemail/ has this to say about comments within an email.

Comments are text surrounded by parentheses (like these). These are OK but don't form part of the address. In other words mail sent to first.last@example.com will go to the same place as first(a).last(b)@example(c).com(d). Strange but true.

I tried entering an url such as "google(ignore).com" but firefox and some other browsers failed and i was wondering is this because its it wrong or is it because they dont know about host name comments ?


That syntax -- comments within an addr-spec -- was indeed permissible by the original email RFC, RFC 822. However, the placement of comments like you'd like to use them was deprecated when that RFC was revised by RFC 2822... 10 years ago. It's still marked as obsolete in the current version, RFC 5322. There's no good excuse for emitting anything using that syntax.

Address parsers are supposed to be backwards-compatible in order to cover all conceivable cases, including 10-years-deprecated bits like the one you're trying to take advantage of here. But I'll bet that many, many receiving mail agents will fail to properly parse out those comments. So even though you may have technically found a loophole via the "obsolete addressing" section of the RFC, it's not likely to do you much good in practice.

As for HTTP, the syntax rules aren't the same as email syntax rules. As you're seeing, the comment section from RFC 822 isn't applicable.


Just because you can do it in the spec, doesn't mean that you should. For example, Gmail will not accept that comment format for address.

Second, (to your last point), paren-comments being allowed in email addresses doesn't mean that they work for URLs.

Finally, my advice: I'd tailor the completeness of your validator to your requirements. If you're writing a new MTA (mail transfer agent), you'll probably have to do it all. If you're writing a validator for a user input, keep it simple:

  • look for one @,
  • make sure you have stuff before (username) and after (domain name),
  • make sure you have a "dot" in the hostname string,
  • [extra credit] do a DNS lookup of the hostname to make sure it resolves.
0

精彩评论

暂无评论...
验证码 换一张
取 消