Mailing List Archive

Checking if email is valid
Hi,

I'm building a simple project using smtplib and have a question. I've been doing unit testing but I'm not sure how to check if an email message is valid. Using regex sounds like a bad idea to me and the other options I found required paying for third party services.

Could someone push me in the right direction please? I just want to find out if a string is a valid email address.

Thank you.

Simon.
Re: Checking if email is valid [ In reply to ]
OK. I've been doing some reading and that you should avoid regex to check email addresses. So what I was thinking was something like this:

if type(email_recipient) != email.message.Message:

I just don't know why that particular line isn't working.

Thank you!

------- Original Message -------
On Wednesday, 1 November 2023 at 10:09, Simon Connah <simon.n.connah@protonmail.com> wrote:


>

>

> Hi,
>

> I'm building a simple project using smtplib and have a question. I've been doing unit testing but I'm not sure how to check if an email message is valid. Using regex sounds like a bad idea to me and the other options I found required paying for third party services.
>

> Could someone push me in the right direction please? I just want to find out if a string is a valid email address.
>

> Thank you.
>

> Simon.
Re: Checking if email is valid [ In reply to ]
On Thu, 2 Nov 2023 at 05:21, Simon Connah via Python-list
<python-list@python.org> wrote:
>
> Could someone push me in the right direction please? I just want to find out if a string is a valid email address.

There is only one way to know that a string is a valid email address,
and that's to send an email to it.

What is your goal though? For example, if you're trying to autolink
email addresses in text, you don't really care whether it's valid,
only that it looks like an address.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
On 2023-11-01, Chris Angelico <rosuav@gmail.com> wrote:
> On Thu, 2 Nov 2023 at 05:21, Simon Connah via Python-list
><python-list@python.org> wrote:
>> Could someone push me in the right direction please? I just want to
>> find out if a string is a valid email address.
>
> There is only one way to know that a string is a valid email address,
> and that's to send an email to it.
>
> What is your goal though? For example, if you're trying to autolink
> email addresses in text, you don't really care whether it's valid,
> only that it looks like an address.

There's often value in even only partially-effective checks though.
With an email address you can easily check to see if it has an "@",
and if the stuff after the "@" is a syntactically valid domain name.
You can also go a bit further and check to see if the domain has an
MX record, and if it doesn't then it is extremely unlikely that the
address is valid.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
On Thu, 2 Nov 2023 at 06:02, Jon Ribbens via Python-list
<python-list@python.org> wrote:
>
> On 2023-11-01, Chris Angelico <rosuav@gmail.com> wrote:
> > On Thu, 2 Nov 2023 at 05:21, Simon Connah via Python-list
> ><python-list@python.org> wrote:
> >> Could someone push me in the right direction please? I just want to
> >> find out if a string is a valid email address.
> >
> > There is only one way to know that a string is a valid email address,
> > and that's to send an email to it.
> >
> > What is your goal though? For example, if you're trying to autolink
> > email addresses in text, you don't really care whether it's valid,
> > only that it looks like an address.
>
> There's often value in even only partially-effective checks though.
> With an email address you can easily check to see if it has an "@",
> and if the stuff after the "@" is a syntactically valid domain name.
> You can also go a bit further and check to see if the domain has an
> MX record, and if it doesn't then it is extremely unlikely that the
> address is valid.

Yeah, which is why I asked about the goal. (You may also note that I
worded the prior statement very carefully: You cannot know that it IS
valid without sending email to it, but there can be ways to know that
it CANNOT BE valid.)

In the vast majority of contexts, local addresses can be ignored, so
an email address will have to include an at sign. I wouldn't bother
with a syntax check on the domain portion, though; just check for a
couple of possible formats (IP literal), and if it looks like a domain
name, do the MX lookup. That said, though, there are certain contexts
where you can be a LOT more restrictive, such as the autolinking
example I mentioned; it's fine to exclude some unusual email addresses
when all you're doing is offering a small convenience.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
On 11/1/23 05:35, Simon Connah via Python-list wrote:
> OK. I've been doing some reading and that you should avoid regex to check email addresses. So what I was thinking was something like this:

To be a little more specific, Avoid Rolling Your Own RegEx. It's very
tricky, and you will get it subtly wrong.

All depending, as others have said, on what level of "validation" you're
after.

--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
On 02/11/2023 00.35, Simon Connah via Python-list wrote:
> OK. I've been doing some reading and that you should avoid regex to check email addresses.

This operation used to be a BIG THING back in the days of 'everyone'
building PHP web-sites. When there were only a handful of TLDs
(top-level domain names, eg the country-codes* such as .nz and .uk plus
the generics such as .com and .net. Accordingly, one could hold the
entire list for convenience of checking. However when IANA (the outfit
responsible for the Root Zone Database and especially ICANN realised
that there was money to be made, they allowed more and more TLDs.
Keeping-up became a rat-race!

Indeed, I've held another .info domain for twenty years, and used to
suffer for my 'bleeding edge' daring - quite recently someone (who
should remain name-less, and a few other less-es) sent me an email
telling me how to log-in, but their log-in process wouldn't accept the
'illegal' address. Ummmmmmmmm...

All together now: "a little bit of knowledge is a dangerous thing"!


So what I was thinking was something like this:
>
> if type(email_recipient) != email.message.Message:
>
> I just don't know why that particular line isn't working.

Will need more context. What is the objective? What is the
source/pre-processing of these data-items. (etc)



* Left-out .au, (a less important geo-TLD) to wind-up @Chris...

--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
On 2023-11-01, Mats Wichmann <mats@wichmann.us> wrote:
> On 11/1/23 05:35, Simon Connah via Python-list wrote:
>> OK. I've been doing some reading and that you should avoid regex to check email addresses. So what I was thinking was something like this:
>
> To be a little more specific, Avoid Rolling Your Own RegEx. It's very
> tricky, and you will get it subtly wrong.

Use e.g.: https://gitea.ksol.io/karolyi/py3-validate-email

--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
On 2023-11-01, Simon Connah via Python-list <python-list@python.org> wrote:

> I'm building a simple project using smtplib and have a
> question. I've been doing unit testing but I'm not sure how to check
> if an email message is valid.

Send an e-mail using it? If the right person gets the e-mail, then
it's valid?

> Using regex sounds like a bad idea to me and the other options I
> found required paying for third party services.
>
> Could someone push me in the right direction please? I just want to
> find out if a string is a valid email address.

You'll have to define "valid". Valid syntactically according to
<what>? Will be accepted by an SMTP server somewhere? Corresponds to
a real person?

Make sure it has an '@' in it. Possibly require at least one '.'
after the '@'.

Trying to do anything more than that is just wasting your time and
annoying the mule.

--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
On Thu, 2 Nov 2023 at 08:09, Grant Edwards via Python-list
<python-list@python.org> wrote:
> Make sure it has an '@' in it. Possibly require at least one '.'
> after the '@'.

No guarantee that there'll be a dot after the at. (Technically there's
no guarantee of an at sign either, but email addresses without at
signs are local-only, so in many contexts, you can assume there needs
to be an at.)

So the regex to match all valid email addresses that aren't local-only
is... drumroll please...

r"@"

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
On 2023-11-01, Chris Angelico via Python-list <python-list@python.org> wrote:
> On Thu, 2 Nov 2023 at 08:09, Grant Edwards via Python-list
><python-list@python.org> wrote:

>> Make sure it has an '@' in it. Possibly require at least one '.'
>> after the '@'.
>
> No guarantee that there'll be a dot after the at.

Ah, I forgot about defaulting to a local domain if one is
omitted. Will MTAs do that these days?

> (Technically there's no guarantee of an at sign either, but email
> addresses without at signs are local-only, so in many contexts, you
> can assume there needs to be an at.)
>
> So the regex to match all valid email addresses that aren't
> local-only is... drumroll please...
>
> r"@"

Unless you want to support UUCP or X400 addresses...

:)




--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
On Thu, 2 Nov 2023 at 08:52, Grant Edwards via Python-list
<python-list@python.org> wrote:
>
> On 2023-11-01, Chris Angelico via Python-list <python-list@python.org> wrote:
> > On Thu, 2 Nov 2023 at 08:09, Grant Edwards via Python-list
> ><python-list@python.org> wrote:
>
> >> Make sure it has an '@' in it. Possibly require at least one '.'
> >> after the '@'.
> >
> > No guarantee that there'll be a dot after the at.
>
> Ah, I forgot about defaulting to a local domain if one is
> omitted. Will MTAs do that these days?

Yeah they will; but that'll depend on the exact server you send to,
whereas if you have a domain part, all you need is some server that
accepts mail for forwarding.

> > (Technically there's no guarantee of an at sign either, but email
> > addresses without at signs are local-only, so in many contexts, you
> > can assume there needs to be an at.)
> >
> > So the regex to match all valid email addresses that aren't
> > local-only is... drumroll please...
> >
> > r"@"
>
> Unless you want to support UUCP or X400 addresses...
>
> :)

Yyyyyyyyeah I think we can assume SMTP these days :)

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
On 01Nov2023 14:08, Grant Edwards <grant.b.edwards@gmail.com> wrote:
>On 2023-11-01, Simon Connah via Python-list <python-list@python.org> wrote:
>> I'm building a simple project using smtplib and have a
>> question. I've been doing unit testing but I'm not sure how to check
>> if an email message is valid.
[...]
>> Could someone push me in the right direction please? I just want to
>> find out if a string is a valid email address.

I confess that I punt "syntactically valid" to email.utils.getaddresses:
https://docs.python.org/3/library/email.utils.html#email.utils.getaddresses

"Deliverable"? I'm prepared to just send to it and hope not to get a
bounce; delivery of email is, after all, asynchronous. (Even if it were
going direct to the primary MX, I still hand it to the local mail system
to deal with.)

"A real person or entity"? A lot of systems do the round trip thing:
here's a special unique and opaue URL,please visit it to confirm receipt
of this email (implying email is 'real"). You see this a lot when
signing up for things. And for plenty of things I generate a random
throw away address at mailinator.com (looking at you, every "catch up"
free online TV streaming service who still wants me to log in).

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
On 11/1/23 04:09, Simon Connah via Python-list wrote:
> Hi,
>
> I'm building a simple project using smtplib and have a question. I've been doing unit testing but I'm not sure how to check if an email message is valid. Using regex sounds like a bad idea to me and the other options I found required paying for third party services.
>
> Could someone push me in the right direction please? I just want to find out if a string is a valid email address.

If I had a nickle for every time a web site claimed my email address
wasn't valid I'd be a rich person. Seems like most attempts at solving
this little problem fall short!
--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
On 2023-11-01 17:17, Chris Angelico via Python-list wrote:
> On Thu, 2 Nov 2023 at 08:09, Grant Edwards via Python-list
> <python-list@python.org> wrote:
>> Make sure it has an '@' in it. Possibly require at least one '.'
>> after the '@'.
>
> No guarantee that there'll be a dot after the at. (Technically there's
> no guarantee of an at sign either, but email addresses without at
> signs are local-only, so in many contexts, you can assume there needs
> to be an at.)

druid!darcy - doesn't work any more but not because it is syntactically
incorrect.

Remember the good old days when we were able to test if an address
existed without sending? That was before the black hats discovered the
Internet.

--
D'Arcy J.M. Cain
System Administrator, Vex.Net
http://www.Vex.Net/ IM:darcy@Vex.Net
VoIP: sip:darcy@Vex.Net

--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
See https://www.linuxjournal.com/article/9585?page=0,0

On 01/11/2023 17:09, Simon Connah via Python-list wrote:
> Hi,
>
> I'm building a simple project using smtplib and have a question. I've been doing unit testing but I'm not sure how to check if an email message is valid. Using regex sounds like a bad idea to me and the other options I found required paying for third party services.
>
> Could someone push me in the right direction please? I just want to find out if a string is a valid email address.
>
> Thank you.
>
> Simon.
>
>

--
Ian Hobson
Tel (+66) 626 544 695
--
https://mail.python.org/mailman/listinfo/python-list
RE: Checking if email is valid [ In reply to ]
Yes, it would be nice if there was a syntax for sending a test message sort
of like an ACK that is not delivered to the recipient but merely results in
some status being sent back such as DELIVERABLE or NO SUCH USER or even
MAILBOX FULL.

An issue with the discussion that may be worth considering is that some
email addresses are not always valid or may not be valid yet but will be
activated later. If I plan on opening a business unit which DNS will later
support as specific.category.mycompany.com.au and we first want to write
some code and test it and roll everything out later, then a test for
user@specific.category.mycompany.com.au could fail some tests now but may be
fine later. Or what if I turn my machine off on weekends and when it boots,
it sets up to be able to receive mail. Is the address only sometimes valid?

We cannot be sure what rules may change and for all we know, they will
select other UNICODE symbols to replace @ for use by countries not having an
@ on keyboards in the local language or support some syntax like {AT} to be
usable ...

I even wonder about a service along the lines of tinyurl where you register
a potentially long or complex or hard to type name and get a short readable
one instead that is just used to provide a re-direct or even changed
periodically to dynamically point to where you want them now, such for the
current day of the week. I can easily imagine them making a funny looking
email address such as user@TINYqwerty that may not pas your current test or
one that looks valid to you but maps into an invalid or even null address.

BTW, checking if an email is valid is much wider as a concept than whether
the email address looks like a possible address. A big check sometimes made
if if the headers in the message and various formatting issues look
reasonable or issues about attachments and even if it is passed by SPAM
detectors. This discussion is just about if an email address LOOKS possibly
valid or should not be accepted.

I note earlier iterations of email had addressed like
mach1!mach2!mach3!ihnp4!mach5!mach6!user or even mach1!mach2!user@mach3 and
I remember tools that analyzed what other machines various machines claimed
to have a direct connection to and tried to figure out a connection from
your source to destination, perhaps a shorter one or maybe a less expensive
one. Hence machines like ihnp4 and various universities that were densely
connected to others got lots of traffic. In that scenario, validity had
another meaning.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of D'Arcy Cain via Python-list
Sent: Wednesday, November 1, 2023 9:57 PM
To: python-list@python.org
Subject: Re: Checking if email is valid

On 2023-11-01 17:17, Chris Angelico via Python-list wrote:
> On Thu, 2 Nov 2023 at 08:09, Grant Edwards via Python-list
> <python-list@python.org> wrote:
>> Make sure it has an '@' in it. Possibly require at least one '.'
>> after the '@'.
>
> No guarantee that there'll be a dot after the at. (Technically there's
> no guarantee of an at sign either, but email addresses without at
> signs are local-only, so in many contexts, you can assume there needs
> to be an at.)

druid!darcy - doesn't work any more but not because it is syntactically
incorrect.

Remember the good old days when we were able to test if an address
existed without sending? That was before the black hats discovered the
Internet.

--
D'Arcy J.M. Cain
System Administrator, Vex.Net
http://www.Vex.Net/ IM:darcy@Vex.Net
VoIP: sip:darcy@Vex.Net

--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
On Thu, 2 Nov 2023 at 15:20, AVI GROSS via Python-list
<python-list@python.org> wrote:
>
> Yes, it would be nice if there was a syntax for sending a test message sort
> of like an ACK that is not delivered to the recipient but merely results in
> some status being sent back such as DELIVERABLE or NO SUCH USER or even
> MAILBOX FULL.
>

Yes, it would! Spammers would be able to use this syntax to figure out
exactly which addresses actually have real people connected to it. It
would save them so much trouble! Brilliant idea.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
Chris Angelico <rosuav@gmail.com> writes:

On Thu, 2 Nov 2023 at 15:20, AVI GROSS via Python-list
<python-list@python.org> wrote:

> Yes, it would be nice if there was a syntax for sending a test
> message sort of like an ACK that is not delivered to the recipient
> but merely results in some status being sent back such as
> DELIVERABLE or NO SUCH USER or even MAILBOX FULL.

Yes, it would! Spammers would be able to use this syntax to figure out
exactly which addresses actually have real people connected to it. It
would save them so much trouble! Brilliant idea.

That sounds like the SMTP "VRFY" command. And spammers _did_ abuse it
in exactly this manner. And so pretty much every mail server in the
world disabled VRFY sometime in the 90s.

- Alan
--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
On 02Nov2023 17:04, Chris Angelico <rosuav@gmail.com> wrote:
>On Thu, 2 Nov 2023 at 15:20, AVI GROSS via Python-list
><python-list@python.org> wrote:
>> Yes, it would be nice if there was a syntax for sending a test
>> message sort
>> of like an ACK that is not delivered to the recipient but merely results in
>> some status being sent back such as DELIVERABLE or NO SUCH USER or even
>> MAILBOX FULL.
>
>Yes, it would! Spammers would be able to use this syntax to figure out
>exactly which addresses actually have real people connected to it. It
>would save them so much trouble! Brilliant idea.

Hmm. IIRC...

https://datatracker.ietf.org/doc/html/rfc2821#section-4.1.1.6

I think a lot of mail receivers don't honour this one, for exactly the
reasons above.

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
>

> On 2023-11-01, Simon Connah via Python-list python-list@python.org wrote:
>

> > I'm building a simple project using smtplib and have a
> > question. I've been doing unit testing but I'm not sure how to check
> > if an email message is valid.
>

>

> Send an e-mail using it? If the right person gets the e-mail, then
> it's valid?
>

> > Using regex sounds like a bad idea to me and the other options I
> > found required paying for third party services.
> >

> > Could someone push me in the right direction please? I just want to
> > find out if a string is a valid email address.
>


OK. It is going to take me some time to get round to every reply here so please bear with me.

Basically I'm writing unit tests and one of them passess in a string with an invalid email address. I need to be able to check the string to see if it is a valid email so that the unit test passess.

>

> You'll have to define "valid". Valid syntactically according to
> <what>? Will be accepted by an SMTP server somewhere? Corresponds to
>

> a real person?
>

> Make sure it has an '@' in it. Possibly require at least one '.'
> after the '@'.
>

> Trying to do anything more than that is just wasting your time and
> annoying the mule.
>


Valid as in conforms to the standard. Although having looked at the standard that might be more difficult than originally planned.

Simon.
Re: Checking if email is valid [ In reply to ]
>

>

> On Thu, 2 Nov 2023 at 05:21, Simon Connah via Python-list
> python-list@python.org wrote:
>

> > Could someone push me in the right direction please? I just want to find out if a string is a valid email address.
>

>

> There is only one way to know that a string is a valid email address,
> and that's to send an email to it.
>

> What is your goal though? For example, if you're trying to autolink
> email addresses in text, you don't really care whether it's valid,
> only that it looks like an address.
>


My goal is to make a simple mailing list platform. I guess I could just send email to an address and if it bounces then I can remove it from the database. Thing is I'm not sure how close to a real email address an email has to be in order to be bounced. If it was completely wrong it might just swallowed up.

Simon.
Re: Checking if email is valid [ In reply to ]
>

> On 2023-11-01, Chris Angelico rosuav@gmail.com wrote:
>

> > On Thu, 2 Nov 2023 at 05:21, Simon Connah via Python-list
> > python-list@python.org wrote:
> >

> > > Could someone push me in the right direction please? I just want to
> > > find out if a string is a valid email address.
> >

> > There is only one way to know that a string is a valid email address,
> > and that's to send an email to it.
> >

> > What is your goal though? For example, if you're trying to autolink
> > email addresses in text, you don't really care whether it's valid,
> > only that it looks like an address.
>

>

> There's often value in even only partially-effective checks though.
> With an email address you can easily check to see if it has an "@",
> and if the stuff after the "@" is a syntactically valid domain name.
> You can also go a bit further and check to see if the domain has an
> MX record, and if it doesn't then it is extremely unlikely that the
> address is valid.
> --
> https://mail.python.org/mailman/listinfo/python-list

Apparently UTF-8 characters are allowed in email addresses now. That is going to lead to a whole new level of pain for determining if an email address is correct.

Simon.
Re: Checking if email is valid [ In reply to ]
On Thu, 2 Nov 2023 at 17:47, Simon Connah <simon.n.connah@protonmail.com> wrote:
>
> My goal is to make a simple mailing list platform. I guess I could just send email to an address and if it bounces then I can remove it from the database. Thing is I'm not sure how close to a real email address an email has to be in order to be bounced. If it was completely wrong it might just swallowed up.
>

Every address is completely separate. There is no "closeness". Just
send email to an address.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Checking if email is valid [ In reply to ]
On Thu, 2 Nov 2023 at 17:47, Cameron Simpson via Python-list
<python-list@python.org> wrote:
>
> On 02Nov2023 17:04, Chris Angelico <rosuav@gmail.com> wrote:
> >On Thu, 2 Nov 2023 at 15:20, AVI GROSS via Python-list
> ><python-list@python.org> wrote:
> >> Yes, it would be nice if there was a syntax for sending a test
> >> message sort
> >> of like an ACK that is not delivered to the recipient but merely results in
> >> some status being sent back such as DELIVERABLE or NO SUCH USER or even
> >> MAILBOX FULL.
> >
> >Yes, it would! Spammers would be able to use this syntax to figure out
> >exactly which addresses actually have real people connected to it. It
> >would save them so much trouble! Brilliant idea.
>
> Hmm. IIRC...
>
> https://datatracker.ietf.org/doc/html/rfc2821#section-4.1.1.6
>
> I think a lot of mail receivers don't honour this one, for exactly the
> reasons above.

Yeah, and it also won't tell you if the mailbox is full, or
unattended, or if the email would be rejected or discarded for any
other reason. Which means it's not even all that useful if it IS
implemented.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list

1 2 3 4  View All