Skip to content

Instantly share code, notes, and snippets.

@JuniorPolegato
Created January 20, 2015 12:44
Show Gist options
  • Save JuniorPolegato/3d56b39f2829ff976d35 to your computer and use it in GitHub Desktop.
Save JuniorPolegato/3d56b39f2829ff976d35 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import dns.resolver
import socket
def obter_mx(email):
try:
answers = dns.resolver.query(email.split('@')[1], 'MX')
mxs = [(rdata.preference, str(rdata.exchange))for rdata in answers]
mx = max(mxs)[1]
except Exception as error:
print 'Erro:', repr(error)
mx = None
return mx
def ler_socket(s):
dados = ''
while True:
d = s.recv(1024)
dados += d
if len(d) < 1024:
break
return dados
def comunicar(s, requisicao):
print requisicao
s.sendall(requisicao + '\n')
resposta = ler_socket(s)
print resposta
return resposta
orig = raw_input('E-mail de origem: ')
helo = orig.split('@')[1]
while True:
to = raw_input('Digite um e-mail para testar: ')
if not to:
break
mx = obter_mx(to)
print mx
if not mx:
print 'MX não encontrado!'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(10)
s.connect((mx, 25))
resposta = ler_socket(s)
print resposta
if resposta[:3] != '220':
comunicar(s, 'quit')
s.close()
continue
resposta = comunicar(s, 'HELO %s' % helo)
if resposta[:3] != '250':
comunicar(s, 'quit')
s.close()
continue
resposta = comunicar(s, 'MAIL FROM: <%s>' % orig)
if resposta[:3] != '250':
comunicar(s, 'quit')
s.close()
continue
resposta = comunicar(s, 'RCPT TO: <%s>' % to)
if resposta[:3] == '550':
print '%s -----> Não existe!' % to
if resposta[:3] == '250':
print '%s -----> Existe!' % to
comunicar(s, 'quit')
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment