Costa.Gustavo
Costa.Gustavo

Reputation: 849

Domain and email validation

I need a domain validator and email validator, ie validate if both exist. The company I'm servicing has a website that validates this for them, ensuring they won't send email to a nonexistent mailbox. It would be an email marketing action anyway. They have something basic about excel, but they want a service to be running directly getting a list of information and or transactional so that it checks by lot, speeding up the process. It is a work very similar to what makes this site.

I would like to develop something similar in python rather. I would like to know if such a work is feasible and if so, if anyone could give me some reference.

Upvotes: 1

Views: 2352

Answers (2)

ooi18
ooi18

Reputation: 142

If you want to check if the email is exist or not, it is better to use an online email validation service to do so, since those service are able you to reach the corresponding mail server and verify is the email address exist or not. At here I will recommend you one email validation service call MailboxValidator . It has free plan to start, and is easy to use with. They has developed their own Python package, so you can start off with that. Example code:

import MailboxValidator

mbv = MailboxValidator.SingleValidation('PASTE_API_KEY_HERE')
results = mbv.ValidateEmail('[email protected]')

if results is None:
    print("Error connecting to API.\n")
elif results['error_code'] == '':
    print('email_address = ' + results['email_address'] + "\n")
    print('domain = ' + results['domain'] + "\n")
    print('is_free = ' + results['is_free'] + "\n")
    print('is_syntax = ' + results['is_syntax'] + "\n")
    print('is_domain = ' + results['is_domain'] + "\n")
    print('is_smtp = ' + results['is_smtp'] + "\n")
    print('is_verified = ' + results['is_verified'] + "\n")
    print('is_server_down = ' + results['is_server_down'] + "\n")
    print('is_greylisted = ' + results['is_greylisted'] + "\n")
    print('is_disposable = ' + results['is_disposable'] + "\n")
    print('is_suppressed = ' + results['is_suppressed'] + "\n")
    print('is_role = ' + results['is_role'] + "\n")
    print('is_high_risk = ' + results['is_high_risk'] + "\n")
    print('is_catchall = ' + results['is_catchall'] + "\n")
    print('mailboxvalidator_score = ' + str(results['mailboxvalidator_score']) + "\n")
    print('time_taken = ' + str(results['time_taken']) + "\n")
    print('status = ' + results['status'] + "\n")
    print('credits_available = ' + str(results['credits_available']) + "\n")
else:
    print('error_code = ' + results['error_code'] + "\n")
    print('error_message = ' + results['error_message'] + "\n")

You can find more information at here.

Upvotes: 1

CDJB
CDJB

Reputation: 14516

For validating emails, this depends on how specifically you want to validate them. If you just want to check that the email follows the basic [email protected] format, then you can use a regular expression - if you want to check the domain at the same time to make sure that the mailbox can exist, then using a module is probably your best bet.

pyIsEmail is a good choice - it also has the facility to check if the domain the email address is from is real, and if it also has a valid MX record attached.

from pyisemail import is_email

address = "[email protected]"
bool_result = isemail(address)
bool_result_with_dns = is_email(address, check_dns=True)

Flanker can also be used for this purpose - as well as checking DNS, MX records and ESP grammar for each email, it can take a list of email records and return a tuple containing lists of the valid and invalid emails.

>>> from flanker.addresslib import address
>>>
>>> address.validate_list(['[email protected], [email protected], @mailgun.com'], as_tuple=True)
([[email protected], [email protected]], ['@mailgun.com'])

Upvotes: 2

Related Questions