Sangram
Sangram

Reputation: 354

Check Email is exist on SMTP server in Python?

I created a script for validating Email is really exists on SMTP server all are working Fine but some fake email also send response 250 OK. how can I manage this issue. please help me.

try:
            domain_name = addressToVerify['Email'].split('@')[1]

            records = dns.resolver.query(domain_name, 'MX')
            mxRecord = records[0].exchange
            mxRecord = str(mxRecord)
            host = socket.gethostname()
            server = smtplib.SMTP()
            server.set_debuglevel(0)
            server.connect(mxRecord)
            server.helo(host)
            server.mail('[email protected]')
            code, message = server.rcpt(str(addressToVerify['Email']))

            server.quit()

Upvotes: 0

Views: 1924

Answers (1)

ZorgoZ
ZorgoZ

Reputation: 3569

First of all, email address verification is a feature that was "banned" (ok, right, it is still there, but hardly one enables it in production scenarios) from the email related RFCs, because of obvious reasons: it become something it was never intended for, heaven for spammers. However, it could work only for local accounts. Because this is how email delivery works. The SMTP part of the concept is only for sending, for delivery. It is a relay. The SMTP server has no idea of the addressees because it is only trying to pass the message to the next hop - which can, of course, be the local server, but can as well be any remote one. It is using the DNS MX records to get the other SMTP party's address from the email addresses domain part. If found, it connects to the other server and passes the message. If it is a local one, it contacts the local delivery agent that manages the actual mailboxes. That one will know if the mailbox exists or not. If not, it will react based on its configuration: it might or might not replay with any warning. But all this happens asynchronously, because of the nature of the internet.

However, as you can see, the SMTP server itself is either way unable to tell if the addressee mailbox actually exists - except for local addresses. You can verify MX record yourself, but that is telling you if there is an SMTP server registered to that email domain or not - and check for its availability on the network. Nothing more. But you can practically not expect VRFY to work.

Sidenote: the local agent is not directly bound to any the mailbox protocol. You can have a mailbox without a mailbox protocol server running. It makes little sense, but it is not technically required.

Of course, most email services you can get for any operating system will be complex ones housing SMTP, delivery agent and a POP3 or IMAP/MAPI server.

Upvotes: 2

Related Questions