Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Verify an Email Address in Python Using smtplib

I have been trying to verify an email address entered by the user in my program. The code I currently have is:

server = smtplib.SMTP()
server.connect()
server.set_debuglevel(True)
try:
    server.verify(email)
except Exception:
    return False
finally:
    server.quit()

However when I run it I get:

ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

So what I am asking is how do i verify an email address using the smtp module? I want to check whether the email address actually exists.

like image 409
Ertdes Avatar asked Mar 06 '14 19:03

Ertdes


People also ask

How does Smtplib work in Python?

The smtplib module defines an SMTP client session object that can be used to send mail to any internet machine with an SMTP or ESMTP listener daemon. For details of SMTP and ESMTP operation, consult RFC 821 (Simple Mail Transfer Protocol) and RFC 1869 (SMTP Service Extensions). Availability: not Emscripten, not WASI.

How do I send an email using Smtplib?

Here, we are going to learn how to send a simple basic mail using Python code. Python offers a ` library to send emails- “SMTP lib”. “smtplib” creates a Simple Mail Transfer Protocol client session object which is used to send emails to any valid email id on the internet. The Port number used here is '587'.


1 Answers

Here's a simple way to verify emails. This is minimally modified code from this link. The first part will check if the email address is well-formed, the second part will ping the SMTP server with that address and see if it gets a success code (250) back or not. That being said, this isn't failsafe -- depending how this is set up sometimes every email will be returned as valid. So you should still send a verification email.

email_address = '[email protected]'

#Step 1: Check email
#Check using Regex that an email meets minimum requirements, throw an error if not
addressToVerify = email_address
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 in ' + addressToVerify)
    raise ValueError('Bad Syntax')

#Step 2: Getting MX record
#Pull domain name from email address
domain_name = email_address.split('@')[1]

#get the MX record for the domain
records = dns.resolver.query(domain_name, 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)

#Step 3: ping email server
#check if the email address exists

# 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('[email protected]')
code, message = server.rcpt(str(addressToVerify))
server.quit()

# Assume 250 as Success
if code == 250:
    print('Y')
else:
    print('N')
like image 51
verybadatthis Avatar answered Oct 08 '22 00:10

verybadatthis