Подскажите, пожалуйста, что я делаю не так? Я пытаюсь проверить, существует ли адрес, прежде чем отправлять письмо, но почему-то этот трюк работает только на 25 порте. Если пробовать подключиться к серверу по SSL, то начинается какая-то мистика.
Пример 1. Если коннектится по 25 порту, то всё работает как и ожидается с любым сервером.
Код:
from dns import resolver
import smtplib
email_to_test = "3b4de8efcfcf1eee@gmail.com"
domain_to_test = email_to_test.split('@')[1]
email_from = '83e82997651764f3@yandex.com'
host_from = email_from.split('@')[1]
def get_mx(domain):
try:
r = resolver.Resolver()
r.nameservers = ['8.8.8.8', '8.8.4.4']
mx_records = r.query(domain, 'MX')
mx_records = [exchange.to_text().split() for exchange in mx_records]
except (resolver.NoAnswer, resolver.NXDOMAIN, resolver.NoNameservers):
mx_records = []
return mx_records
def check_smtp(email_to_test, mx_record, email_from, host_from):
print("Creating server")
server = smtplib.SMTP(timeout = 10)
server.set_debuglevel(0)
print("Server defined, trying to connect")
server.connect(mx_record, 25)
print("Connected, trying helo")
server.helo(host_from)
print('Helo is ok, trying mail & rcpt')
server.mail(email_from)
code, message = server.rcpt(email_to_test)
server.quit()
return (code, message)
mx_records = get_mx(domain_to_test)
print('MX records is:')
for record in mx_records:
print(record[1])
print('SMTP test:')
print(check_smtp(email_to_test, mx_records[0][1], email_from,host_from))
Результат:
MX records is:
gmail-smtp-in.l.google.com.
alt3.gmail-smtp-in.l.google.com.
alt1.gmail-smtp-in.l.google.com.
alt4.gmail-smtp-in.l.google.com.
alt2.gmail-smtp-in.l.google.com.
SMTP test:
Creating server
Server defined, trying to connect
Connected, trying helo
Helo is ok, trying mail & rcpt
(550, b"5.1.1 The email account that you tried to reach does not exist. Please try\n5.1.1 double-checking the recipient's email address for typos or\n5.1.1 unnecessary spaces. Learn more at\n5.1.1 https://support.google.com/mail/?p=NoSuchUser p124si2609112oib.236 - gsmtp")
Пример 2. А теперь попробуем с SSL.
Код:
def check_smtp(email_to_test, mx_record, email_from, host_from):
print("Creating server")
server = smtplib.SMTP_SSL(timeout = 10)
server.set_debuglevel(1)
print("Server defined, trying to connect")
server.connect(mx_record, 465)
print("Connected, trying helo")
server.helo(host_from)
print('Helo is ok, trying mail & rcpt')
server.mail(email_from)
code, message = server.rcpt(email_to_test)
server.quit()
return (code, message)
Получаем у Gmail и большинства других серверов socket.timeout:
SMTP test:
Creating server
Server defined, trying to connect
connect: ('alt1.gmail-smtp-in.l.google.com.', 465)
connect: ('alt1.gmail-smtp-in.l.google.com.', 465)
Traceback (most recent call last):
...
socket.timeout: timed out
А вот mail.ru внезапно говорит Connection refused:
SMTP test:
Creating server
Server defined, trying to connect
connect: ('mxs.mail.ru.', 465)
connect: ('mxs.mail.ru.', 465)
Traceback (most recent call last):
...
ConnectionRefusedError: [Errno 61] Connection refused