2

Is there any way to verify an email whether the email actually exist or no in python? Or is thee any platform offering such services? For Example:

I have some emails

email1@google.com

email2@gmail.com

asdasd@adasd.com

asdasdasdasdasd@gmail.com

How can I be 100% sure which of the following email really exist on the internet?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Salman Khan
  • 158
  • 1
  • 7
  • 1
    Send an email to that address and ask the owner to click a verify link. That's why you need to click verify links when you open a Google account or a Facebook account or Stackoverflow etc. – slebetman Oct 01 '21 at 22:31
  • I don't think there's any such service, and it's not really possible to design such a service. – Barmar Oct 01 '21 at 22:31
  • The verify link method only works for email addresses that go to humans. Some email addresses go to automated services. – Barmar Oct 01 '21 at 22:32
  • 2
    No. And the reason it doesn't exist is slimeball spammers. They would love to be able to trim their lists of bogus names. – Tim Roberts Oct 01 '21 at 22:33
  • I edited the post and added example. – Salman Khan Oct 01 '21 at 22:36
  • Additionally many domains implement catch-all addresses so literally anything @ some_domain won't bounce...but it doesn't necessarily end up in anyone's inbox. – Mark Oct 01 '21 at 22:38
  • 1
    That is an odd question. Given a registered domain, **any** email address on that domain exists. You might want to explain what you are really trying to accomplish. – IInspectable Oct 02 '21 at 08:20
  • [Please don't add "solved" to your title or question](https://meta.stackexchange.com/a/116105/248627). Instead, [mark an answer correct by clicking on the checkmark](https://meta.stackexchange.com/q/5234/248627). You can also [add your own answer](https://stackoverflow.com/help/self-answer) and accept it if none of the ones you received solved your problem. (Note that the "solution" link you provided in the question no longer resolves. That's one reason link-only answers are strongly discouraged here.) – ChrisGPT was on strike Nov 20 '22 at 19:32

6 Answers6

5

There are several tutorials on the internet. Here's what I found...


First you need to check for the correct formatting and for this you can use regular expressions like this:

   import re
   
   addressToVerify ='info@scottbrady91.com'
   match = re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', addressToVerify)
   
   if match == None:
      print('Bad Syntax')
      raise ValueError('Bad Syntax')

DNS

Next we need to get the MX record for the target domain, in order to start the email verification process. Note that you are allowed in the RFCs to have a mail server on your A record, but that's outside of the scope of this article and demo script.

    import dns.resolver
   
    records = dns.resolver.query('scottbrady91.com', 'MX')
    mxRecord = records[0].exchange
    mxRecord = str(mxRecord)

Python DNS Python doesn't have any inbuilt DNS components, so we've pulled in the popular dnspython library. Any library that can resolve an MX record from a domain name will work though.

Mailbox

Now that we have all the preflight information we need, we can now find out if the email address exists.

   import socket
   import smtplib
   
   # Get local server hostname
   host = socket.gethostname()
   
   # SMTP lib setup (use debug level for full output)
   server = smtplib.SMTP()
   server.set_debuglevel(0)
   
   # SMTP Conversation
   server.connect(mxRecord)
   server.helo(host)
   server.mail('me@domain.com')
   code, message = server.rcpt(str(addressToVerify))
   server.quit()
   
   # Assume 250 as Success
   if code == 250:
      print('Success')
   else:
      print('Bad')

What we are doing here is the first three commands of an SMTP conversation for sending an email, stopping just before we send any data.

The actual SMTP commands issued are: HELO, MAIL FROM and RCPT TO. It is the response to RCPT TO that we are interested in. If the server sends back a 250, then that means we are good to send an email (the email address exists), otherwise the server will return a different status code (usually a 550), meaning the email address does not exist on that server.

And that's email verification!

source: https://www.scottbrady91.com/Email-Verification/Python-Email-Verification-Script


Here are some other alternatives from another website...

Let’s make it more sophisticated and assume we want the following criteria to be met for an account@domain email address:

  • Both consist of case-insensitive alphanumeric characters. Dashes, periods, hyphens or underscores are also allowed
  • Both can only start and end with alphanumeric characters
  • Both contain no white spaces account has at least one character and domain has at least two domain includes at least one period
  • And of course, there’s an ‘@’ symbol between them
    ^[a-z]([w-]*[a-z]|[w-.]*[a-z]{2,}|[a-z])*@[a-z]([w-]*[a-z]|[w-.]*[a-z]{2,}|[a-z]){4,}?.[a-z]{2,}$

Validating emails with Python libraries

Another way of running sophisticated checks is with ready-to-use packages, and there are plenty to choose from. Here are several popular options:

email-validator 1.0.5

This library focuses strictly on the domain part of an email address, and checks if it’s in an x@y.com format.

As a bonus, the library also comes with a validation tool. It checks if the domain name can be resolved, thus giving you a good idea about its validity.

pylsEmail 1.3.2

This Python program to validate email addresses can also be used to validate both a domain and an email address with just one call. If a given address can’t be validated, the script will inform you of the most likely reasons.

py3-validate-email

This comprehensive library checks an email address for a proper structure, but it doesn’t just stop there.

It also verifies if the domain’s MX records exist (as in, is able to send/receive emails), whether a particular address on this domain exists, and also if it’s not blacklisted. This way, you avoid emailing blacklisted accounts, which would affect deliverability.

source: https://mailtrap.io/blog/python-validate-email/

These are just extracts from these websites, make sure to visit them and confirm this can be useful for your specific case, they also include much more methods and explain this in more detail, hope it helps.

Brian61354270
  • 8,690
  • 4
  • 21
  • 43
Alex
  • 778
  • 1
  • 15
  • 2
    The email validation regex here is incorrect. You can see a more complete email validation regex in this answer: https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression However, you should probably just be validating emails this way at all. – Dietrich Epp Oct 01 '21 at 23:20
1

You can send a verification email with a code to the entered email to verify its presence. This can be done using any python framework. Django, flask, etc.

1

You can use gmass.co, connect to your google account , go to settings , apikeys , then create an api key and use it this way :

sent = requests.get('https://verify.gmass.co/verify?email='+email+'&key='+api,
                              headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'}
                              )
if ("valid" in sent.content):
     print(email+"valid")
else: 
     print(email+"invalid")
sigmarule69
  • 34
  • 1
  • 2
1

There are many services offering such tools that can validate your emails in bulk. If you are doing this for a marketing campaign to avoid bounce rate and getting your campaign marked as spammed then there are services that can help you checking bulk emails for your marketing campaigns. However, there is some online platform too but you can't check multiple emails.

Validating email addresses is easy using valid email checkers. You can use SMTPlib, a python library used for sending emails and using python regex too. But It will only check for the email if it is an email or something else.

import re

regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
def check(email):

    if(re.fullmatch(regex, email)):
        print("Valid Email")
 
    else:
        print("Invalid Email")

if __name__ == '__main__':
    email = input("Enter Email:")
    check(email)

This is not the actual solution to your question, but an example of matching email with regex. Check out the services that offer emails validation in bulk. I'd recommend EmailChecks

0

The best method would be to send a verification mail, with some code they need to submit. Not only this is simple, it also proves the email belongs to them

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 23 '22 at 03:20
0

I believe there are three ways to approach this.

1. REGEX

Other thread participants have already suggested this. The problem with this approach is that it simply checks a string for the correct syntax and does not ensure that the email address exists.

2. Bulk Software

There are plenty of GUI tools that allow you to import lists that are then being validated. However, this usually does not fit the software development case.

3. Email Validation API

The previously mentioned GUI tools often offer an API that allows you to conduct real-time validation of your user's email addresses.

A few vendors that provide this: