0

I try to verify email and I have some difficulties:

Often I get some errors, for example

dns.resolver.NoAnswer: The DNS response does not contain an answer to the question: siemens.ru. IN MX

What does it mean? I can't check email or another? Or I should try again?

smtplib.SMTPServerDisconnected: Connection unexpectedly closed

Is any way to solve that problem? It appears very often.

dns.resolver.NXDOMAIN: None of DNS query names exist: aruanaestetik.com., aruanaestetik.com.

If I get this error, I should skip email with this domain, right?

Also when I try get mxrecords, sometimes the number of them more than one. Should I use every of it to check email or I can use a random one?

I use next code to do that:

    resolver = dns.resolver.Resolver()
    resolver.timeout = 60
    resolver.lifetime = 60
    mx_records = resolver.query(dom, 'MX')
    mxRecord = str(mx_records[0].exchange)
    host = socket.gethostname()
    server = smtplib.SMTP()
    server.set_debuglevel(0)
    server.connect(mxRecord)
    server.helo(host)
    server.mail('me@domain.com')
    code, message = server.rcpt(str(addressToVerify))
    server.quit()

Also I only specify my mail in the server.mail(), but I try to login there and after that checking email, but don't get any result so I log out. Why does it happen?

Petr Petrov
  • 4,090
  • 10
  • 31
  • 68

2 Answers2

1

did you check mx manually?

It seems like port 25 is not open for connection and no mx record could be found, so it returns the error that no mx record exists when doing a manual check for aruanaestetik.com one gets the message no mx recorf found

Shubham Namdeo
  • 1,845
  • 2
  • 24
  • 40
minime
  • 11
  • 4
  • No, I use `dns.resolver.query(domain, 'MX')` to do that. Can you explain, if domain contains a lot of `mx`, chould I check email with every of it? – Petr Petrov Feb 09 '17 at 08:54
  • dns.resolver.query(domain, 'MX') return the error you asked about when no mx could be found and/or p25 is closed, yes you could check email with every of it but only one might allow login when you try to send – minime Feb 09 '17 at 09:01
1

There will be no code in my answer, I'd like to describe the process and focus on error handling.

An email address is of the form user@domain.com

To check the domain part, you have to obtain the MX records from DNS. If (and only if) there are no MX's, an A record should be used instead. I would consider such case as a misconfigured mail system, though.

When doing DNS lookup, two groups of error may occur: transient ones (e.g. a timeout) and persistent ones (e.g. NXDOMAIN). In the case of transient errors, the lookup should be repeated later.

There are usually multiple MX records. They have a numeric priority. Smaller number = higher priority. Mail always travels from higher MX number to lower MX number.

In order to check the user part of an address you have to contact the server with the highest priority. All others are backup servers and it is quite common that they have no access to the user database. They simply accept all mail for the own domain.

There is a small chance that your sender address will be rejected for whatever reason. Use an empty address <> reserved for error messages.

Again, two groups of error exist when communicating with a SMTP server. Fortunately, the SMTP responses have a 3-digit code. The first digit is 2 for success, 4 for transient errors (to be retried later) and 5 for permanent failures.

And a final note: Do a SMTP RSET before QUIT if you are not going to actually send an email.

VPfB
  • 14,927
  • 6
  • 41
  • 75
  • Thank you for the explanation. But what is the `A record`? – Petr Petrov Feb 09 '17 at 09:26
  • And is any way to "fight" with `smtplib.SMTPServerDisconnected: Connection unexpectedly closed`? – Petr Petrov Feb 09 '17 at 09:30
  • @PetrPetrov An "A" record is a type of DNS records mapping a name to its IP **A**ddress, more precisely IPv4 address. The corresponding IPv6 "A" record is named "AAAA". [https://en.wikipedia.org/wiki/List_of_DNS_record_types] – VPfB Feb 09 '17 at 11:21
  • @PetrPetrov regarding the unexpected close I would debug the SMTP conversation, if it happens too often. I mean to check responses from EHLO, MAIL and RCPT. I'm sorry, I have no better idea. – VPfB Feb 09 '17 at 11:25
  • Thank you) I'll to search more information. You've helped me) – Petr Petrov Feb 09 '17 at 11:30
  • can you say me something else, can different `mx` return different code? or if it return code `250`, another `mx` return it too? – Petr Petrov Feb 09 '17 at 19:25
  • @PetrPetrov You did not wrote different code in reply to what. But regarding the recipient addresses the answer is yes. It is common that the primary SMTP server doing the final delivery checks address thoroughly and returns e.g. "no such user" error while a backup SMTP server does only basic checks and accepts all user names within the domain. The backup SMTP server then stores the mail in a queue on its disk and then forwards it to the primary server which rejects the mail and the mail bounces. – VPfB Feb 09 '17 at 22:13
  • @PetrPetrov (continuation of long comment) Summary: the best answer comes from the primary (highest MX priority) server. If a backup (lower MX priority) permanently rejects an address, then you know the address is not valid. But if it accepts an address, it does not necessary mean that the address is valid. All you can assume is that it was not verified yet at the final destination. – VPfB Feb 09 '17 at 22:14