Email verification via smtp protocol. Find out that there is mail and waiting for letters using python

Useful for solving problems of organizing mailing lists, as well as a means of preliminary verification of email. email upon registration.

This is a retelling of a rather old articles (2015) one Scott Brady. The author warns that an attempt to implement the proposed approach on an industrial scale will lead you to the lists of spammers, and in general, all this is extremely unreliable and doubtful. But more was implemented in order to get acquainted with the process, which I actually propose to do.

The verification process consists of 4 parts:

  1. Syntax check

  2. Domain and MX record check

  3. Checking helo response

  4. And finally, server.rcpt (willingness to accept to a specific address)

Syntax check

There are quite a lot of materials on checking the syntax, you can familiarize yourself with them (1, 2, 3). Scott offers his own expression, which is also quite working.

import re

match = re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', addressToVerify)

Domain and MX record check

Perhaps the most valuable thing in all this material is checking the domain and the existence of email routing on it. Done with the help dnspython libraries.

import dns.resolver

try:
    records = dns.resolver.resolve("mail.ru", 'MX')    
    mxRecord = records[0].exchange
    mxRecord = str(mxRecord)
except Exception as ex:
    print(ex)

According to the experience of using it, this is where most of the errors were, either non-existent domains, or the absence of an MX record. Which automatically means the pointlessness of trying to send mail.

Checking helo response

At the same time, the existence of a domain and the presence of an MX record do not mean that a letter can be sent, you need to start a conversation with the server.

import smtplib

server = smtplib.SMTP()
server.set_debuglevel(0)

try:
    server.connect(mxRecord)
    code, message = server.helo(host)    
except Exception as ex:
    print(ex)   

At this stage, there were no errors to catch. If all is well, the code will be 250, and the server’s response message will be of varying degrees of courtesy. For mail.ru it is ‘mxs.mail.ru’, Google is more friendly and answers:

mx.google.com at your service

Here it is worth noting that the check is carried out without connecting to a real mail server (large services are calm about this, but smaller corporate ones can already cut the request at this stage).

There is also a check ehlo.

Willingness to receive at a specific address

IN smtplib library there is a special SMTP method.verify(address), however, directly in the documentation it is indicated that many services turned it off due to the activities of spammers, so another way is proposed to check the existence of a particular mailbox, namely SMTP.rcpt(address).

server.mail('any@mail.box')
code, message = server.rcpt(str(addressToVerify))

This is a low-level method that identifies the recipient’s envelope. If everything is fine, we get 250 code and a message like:

(250, b'Go ahead')

This place also catches a lot of problems. For example, mail.ru is not shy about saying “Go ahead” with obviously non-existent addresses. Google, gives more meaningful answers on the contents of the envelope.

(550, b”5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient’s email address for typos or\n5.1.1 unnecessary spaces. Learn more at https://support.google.com/mail/?p=NoSuchUser “)

Once again, I note that all checks were performed without connecting to a real mail server, and in some cases a spam assassin does not allow access to the mailbox, there are many more pitfalls.

In short, by and large, this is a manual passage of all stages of sending a letter, except for the actual sending.

Advertising law

Just in case, let me remind you that mailings fall under advertising law and requires prior consent to receive it. Postal services require to indicate in such letters where the consent to receive was received and in capital letters the inscription “UNPOST”.

Links

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *