31

I study regex and faced a question: could an email address like username@192.0.2.1 be valid?

Sure there is the valid username, but what about domain? Should it be domain name only or is host ip address allowed too? Of course I mean any host address, not the localhost only.

8
  • 9
    Hello and welcome. You should use addresses like 192.0.2.1 for your question. These addresses are "example addresses" and it would then be clearer that you are not specifically asking about localhost (instead of you having to point that out separately).
    – Rodney
    Jul 24 at 14:45
  • 15
    As someone that has once done so, I have one piece of advice: Do not, and I repeat, DO NOT try to write a regexp that matches valid email addresses. Even if you don't implement the IPv6 address contraction ("::") syntax validation, the full regexp will be humongous. (Or wrong.)
    – Bass
    Jul 25 at 14:08
  • 12
    @alphacapture Check this Wikipedia article. The IP range 192.0.2.0/24 is assigned for documentation and example purposes. The same holds for IPv6 2001:db8::/32.
    – iBug
    Jul 25 at 19:59
  • 5
    I can only second @Bass. I also tried this once but after reading about all the possibilities that are officially allowed (e.g. you can even have spaces if you quote them), I ended up with just a very basic check, verifying that there is at least one "@".
    – luator
    Jul 26 at 9:24
  • 3
    @user11153 is a@example.com an "exotic e-mail address"? Because I've had sites reject E-Mail addresses using this format as invalid, even thought they are valid and work just fine. This sort of attitude will do nothing but leave your users frustrated.
    – Philippe
    Jul 26 at 15:30
64

No, this would not be a valid email address. The host part of an email address (@example.com), can be an IP address instead of a domain name, but the IP must be enclosed in brackets, for example:

username@[127.0.0.1]

See RFC 5321 Section 4.1.3:

Sometimes a host is not known to the domain name system and communication (and, in particular, communication to report and repair the error) is blocked. To bypass this barrier, a special literal form of the address is allowed as an alternative to a domain name. For IPv4 addresses, this form uses four small decimal integers separated by dots and enclosed by brackets such as [123.255.37.2], which indicates an (IPv4) Internet Address in sequence-of-octets form.

That said, in practice an email address like using an IP like this will trigger spam prevention measures all over, so it wouldn't be very practical. But it is technically a valid email address.

3
  • 3
    If you're setting up you own mail system then it could be useful. As long as you don't try to interoperate with any public providers.
    – OrangeDog
    Jul 26 at 13:33
  • Even if you're setting up your own mail provider, a domain is a better choice for this identifier. There is almost no reason to include an IP with a username as either only one machine would be assigned a given IP or they would be dynamic (DHCP). Even if there is some reason like static IPs for different branches, for auditability I recommend coming up with a readable domain so admins will know what an identifier means, like branch1.mail.example.com. Plus, by using domains and DNS you can use SPF, DKIM and other common technologies that even a custom mail service can benefit from.
    – johnrom
    Jul 26 at 14:27
  • 4
    @johnrom Question asks could an email address like username@127.0.0.1 be valid. The address is valid (if brackets are used on the IP address). It is not deliverable except in extremely special circumstances; that does not affect the validity. Everything you stated in your comment is beside the point if the question is validity. Jul 26 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.