4

I want to verify a large number of emails addresses using Python. I was wondering what would be the best way of doing so. I've looked at the SMTP and the Validate Email library but I haven't been able to get either working.

For the SMTP library I couldn't figure out how to setup an SMTP server(I tried using Gmail's but it didn't work) and for the Validate Email it was giving me "None" to ANY email address I tried.

Validate Email library code:

from validate_email import validate_email
is_valid = validate_email(EMAIL ADDRESS,verify=True)

I always got back "None" from the code above.

SMPT library code:

import smtplib
server = smtplib.SMTP()
server.connect()

When I used server = smtplib.SMTP('smtp.gmail.com',587) I got:

(252, "2.1.5 Send some mail, I'll try my best or1sm11343445igb.4 - gsmtp")

No matter what email I put in server.verify(email).

EDIT 1: Many of the answers only check the form of an email address instead of whether or not the email address is actually valid!

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Soroush Ghodsi
  • 380
  • 1
  • 7
  • 19
  • Questions asking us to recommend or find a book, tool, **_software library_**, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – Arc676 Dec 23 '15 at 03:56
  • Do you mean verifying an email exists in the whole world?... You can't exactly do that but you can verify things about a domain. – Clay Dec 23 '15 at 03:57
  • @Arc676 How is that? – Soroush Ghodsi Dec 23 '15 at 03:59
  • @Clayton I mean to verify for example if "bob@uber.com" actually exists. – Soroush Ghodsi Dec 23 '15 at 04:00
  • 3
    StackOverflow isn't a code writing service. What have you tried? – Arc676 Dec 23 '15 at 04:00
  • You cannot verify that email exists. – Clay Dec 23 '15 at 04:02
  • You described your problem poorly. The start of every good program is a good problem description, even if it only exist in your head. In your case you have even been using terms wrongly. The difference between "emails" and "email addresses" is like the difference between "house" and "house number". – Klaus D. Dec 23 '15 at 04:03
  • Thank you to everyone for pointing out the mistakes with how I stated the problem. I hope I addressed all the issues. – Soroush Ghodsi Dec 23 '15 at 04:06
  • @KlausD. Thank you for pointing that out. Does the problem look better now? – Soroush Ghodsi Dec 23 '15 at 04:07
  • @Arc676 I hope I have now solved that issue! – Soroush Ghodsi Dec 23 '15 at 04:08
  • Post your code. Maybe there's a bug in your code and _that_ is something we can help with (as long as it isn't caused by a typo, which makes it off topic). – Arc676 Dec 23 '15 at 04:10
  • 1
    @Arc676 I have added the code for both library's I tried. – Soroush Ghodsi Dec 23 '15 at 04:17
  • @intboolstring I don't want to validate an email's form but whether or not it's a valid email that really exsists. – Soroush Ghodsi Dec 23 '15 at 04:19
  • 1
    Ok. That was what I was wondering if you were asking. – intboolstring Dec 23 '15 at 04:19
  • This is a very common FAQ. You cannot reliably verify that an address is valid without interacting with its owner. I could not find a duplicate specifically for Python but I will nominate one of the many others with a good answer. – tripleee Dec 23 '15 at 05:36
  • 1
    Possible duplicate of [verify email address on Linux](http://stackoverflow.com/questions/20343468/verify-email-address-on-linux) – tripleee Dec 23 '15 at 05:37
  • Thank you @tripleee will look into that. I'm also not that worried about it's accuracy as long as it works some percent of the time. – Soroush Ghodsi Dec 23 '15 at 05:48
  • @tripleee I've looked at it and I'd really need a faster solution and also preferably using Python. I also have been getting deliverable for everything I've tried. – Soroush Ghodsi Dec 23 '15 at 06:40
  • I have not examined the library you were having trouble with, but in order to do anything remotely useful, it will need unrestricted outbound access on port 25, which is virtually nonexistent on consumer IP addresses. – tripleee Dec 23 '15 at 17:37

1 Answers1

0

It's practically impossible to verify the validity/existence of an email address with a 100% accuracy.

It is also very tough to reliably match it using regex. Take a look at this question:Using a regular expression to validate an email address

This is what seems the most practical way to go about validation. Use simple regex validation something@something to weed out common errors. This can be done using the following python code:

import re

valid_re = re.compile(r'^.+@.+')
# or something like re.compile(r'\S+@\S+')
def validate_email(email):
    if(valid_re.match(email)):
        # valid
    else:
        # invalid

After which, the best you can do is send a confirmation email and wait for a response.

Community
  • 1
  • 1
oxalorg
  • 2,768
  • 1
  • 16
  • 27
  • I'm looking to verify an email address exists, not just verify it's form! – Soroush Ghodsi Dec 23 '15 at 16:56
  • @Soroush I'm not sure if you read the above answer, but It's practically impossible to verify the validity/existence of an email address with a 100% accuracy. The best you can do is send a confirmation email and wait for a response. :) Sorry if that didn't help. – oxalorg Dec 23 '15 at 17:47