0

I have a RegEx for validating email addresses, but I'm really looking to validate a whole From header. Any of these would be valid:

[email protected]
<[email protected]>
My Name <[email protected]>

Is there anything out there that would validate these as valid from headers? I'm going to look in the smtp library :)

3
  • I have to ask, why do you need to validate the headers yourself rather than letting an SMTP server do it for you? It's a rather complicated task to do the validation properly. Commented Jul 16, 2010 at 19:54
  • If you want to make sure you're validating everything properly, check the standard format: faqs.org/rfcs/rfc2822.html Commented Jul 16, 2010 at 21:54
  • We're supplying our own from address, so I need to make sure the email editors supply a proper from header/email address, otherwise the emails they send will fail. Commented Jul 20, 2010 at 4:12

2 Answers 2

1

I couldn't get the posted response to work, so I've been working on this and finally got this one, which seems to work so far. I'm sure it'll miss/catch something, but it's working for now.

[a-zA-Z0-9+_\-\.\ ]*[ ]*<?[a-zA-Z0-9+_\-\.]+@[0-9a-zA-Z][.-0-9a-zA-Z]*.[a-zA-Z]+>?
Sign up to request clarification or add additional context in comments.

Comments

1

Be aware that there are plenty of other valid cases of e-mail addresses beyond what you've posted.

See here for a recipe that may help. Also read this for a great discussion of parsing email addresses with a regex. There are any number of good regexes in there that will match the uses you're looking for, imho :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.