Created
March 1, 2023 08:35
-
-
Save elydev01/523ea4f04f072354a7fa17f92cc43807 to your computer and use it in GitHub Desktop.
Vérifier si une adresse email est correct et existe réellement en python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Avec la bibliothèque `validate_email` | |
from validate_email import validate_email | |
def is_valid_email(email): | |
is_valid = validate_email(email, verify=True) | |
return is_valid | |
# En utilisant une requête SMTP | |
import smtplib | |
import dns.resolver | |
def is_valid_email(email): | |
# Séparer le nom d'utilisateur et le nom de domaine | |
username, domain = email.split('@') | |
try: | |
# Vérifier le nom de domaine | |
mx_records = dns.resolver.query(domain, 'MX') | |
mx_record = mx_records[0].exchange.to_text() | |
# Établir une connexion SMTP | |
smtp = smtplib.SMTP() | |
smtp.connect(mx_record) | |
smtp.helo(smtp.local_hostname) | |
smtp.mail('test@test.com') | |
code, message = smtp.rcpt(email) | |
smtp.quit() | |
# Vérifier le code de retour | |
if code == 250: | |
return True | |
else: | |
return False | |
except: | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment