简体   繁体   English

以编程方式识别和验证邮件服务器

[英]Identify & Validate Mail Server, Programmatically

Like did in the mailtester website , I have to identify & validate-the-existance-of mail server, programmatically. 就像在mailtester网站上所做的一样,我必须以编程方式识别并验证邮件服务器的存在。

I've checked the web deeply but I don't see any tutorial, how-to-do guide, api, library, or online course. 我已经仔细检查了网络,但没有看到任何教程,操作指南,API,库或在线课程。

I will not use the program for any spam purpose like done in the mentioned website (no declared privacy issues about anti-spam, or no declaration for illegal usage & storage of valid email address) . 不会将程序用于任何垃圾邮件用途,如在上述网站中所做的那样(没有关于反垃圾邮件的已声明隐私问题,也没有关于非法使用和存储有效电子邮件地址的声明) By the way, mail server identification is small part of the project I'm now working on, but I have to implement it. 顺便说一句,邮件服务器识别只是我现在正在研究的项目的一小部分,但是我必须实现它。

Please, answer any of the, but all in the same direction, questions: 请回答所有但都朝着相同方向的问题:

  • Where should I start so as to implement my to-do? 我应该从哪里开始,以便执行我的待办事项?
  • Any tutorial, how-to-do guide, api, library, or online course? 有任何教程,操作指南,API,库或在线课程吗?
  • What is the proper way to implement above to-do, programmatically? 以编程方式实现上述任务的正确方法是什么?

Note: You can check the screenshot, which is taken from mentioned website. 注意:您可以查看屏幕截图,该屏幕截图来自提到的网站。

邮件测试仪的屏幕截图

Do I correctly interpret your question: 我是否正确解释您的问题:

You want to check whether SMTP server (from which some mail arrived for example) exists and you wish to check whether that mail exists as well. 您要检查是否存在SMTP服务器(例如,从中到达某些邮件的服务器),并且还希望检查该邮件是否也存在。

To do so, you can do few things: 为此,您可以做一些事情:

Solution 1: 解决方案1:

ping the server, for instance mail.example.com, ping服务器,例如mail.example.com,

if server responds, then check whether ports that are usually used by SMTP are opened: usually 25 如果服务器响应,则检查是否打开了SMTP通常使用的端口:通常25

if at least one of them is opened then you can say that mail server on this domain is running. 如果其中至少有一个打开,则可以说该域上的邮件服务器正在运行。

This solution has a problem that some servers won't respond to ping. 该解决方案存在一些服务器无法响应ping的问题。 But instead of pinging it, you can request the host name from DNS to see whether you'll get IP for a domain. 但是,您可以从DNS请求主机名,而不是对其进行ping操作,以查看是否将获得域的IP。 Then this solution has a problem that a server might have been there yesterday but it is not today, and DNS returns the IP, but nobody is at the end of it. 然后,此解决方案存在一个问题,即服务器可能昨天已经在那儿了,但今天不是今天,并且DNS返回了IP,但是没有人在后面。

This you can achieve by calling ping, hostname, netmap and similar tools using subprocess or by programing it all yourself from scratch with sockets. 您可以通过使用子进程调用ping,主机名,netmap和类似工具,或者使用套接字从头开始对它们进行编程来实现此目的。

You can, off course, open a socket to the domain, directly to port 25, but you'll have to specify timeout because it can happen that whole thing blocks because no answer is coming from server. 当然,您可以打开域的套接字,直接打开端口25,但是您必须指定超时,因为可能会发生整个事件阻塞的情况,因为服务器没有答案。 Yes, pinging is an extra step, but it is faster in cases where server doesn't exist. 是的,ping是一个额外的步骤,但是在不存在服务器的情况下它可以更快。 It just sends ICMP packets, it doesn't try any handshakes. 它只是发送ICMP数据包,不尝试任何握手。 (I know from experience). (我从经验中知道)。

Solution 2: 解决方案2:

More acceptable solution is to use smtplib just to try connecting to that server. 更可接受的解决方案是仅使用smtplib尝试连接到该服务器。

It will "attack" the specified domain asking on default port and sending an SMTP request. 它将“攻击”指定的域,询问默认端口并发送SMTP请求。 The exception will be raised if the server is not SMTP or if it doesn't exist. 如果服务器不是SMTP或不存在,则会引发异常。 Timeout also should be set. 超时也应该设置。 Pinging will here also increase speed of checks, and no timeouts needed. 此处的ping操作还将提高检查速度,并且不需要超时。 You just assume that if server doesn't answer, it doesn't exist. 您仅假设服务器不应答,则该服务器不存在。 (Hm, cleverness of this aproach is questionable) (嗯,这个方法的巧妙性值得怀疑)

If anything, a firewall, filter or something alike, doesn't allow you any of those checks, you can always have an updated lists of all mail servers (if it can be optained from any robot) stored locally or connect to a service like one you mentioned to consult it about the address. 如果防火墙,过滤器之类的东西不允许您进行任何此类检查,则始终可以在本地存储所有邮件服务器的更新列表(如果可以从任何机械手进行访问),或者可以连接到类似您提到的一个地址。

Solution 3: 解决方案3:

Last resort solution for checking if an address exists is to try using that SMTP server to send ourselves an e-mail. 检查地址是否存在的最后一种解决方案是尝试使用该SMTP服务器向自己发送电子邮件。 If thing doesn't work, then e-mail doesn.t exist. 如果事情不起作用,那么电子邮件就不存在。

Big problem!!!: Most SMTPs doesn't ask for login yes, but what if this one does? 大问题!!!:大多数SMTP都不要求登录,但是如果这样的话怎么办? And, most SMTP servers allow you to send an e-mail from any address that doesn't even exist! 而且,大多数SMTP服务器都允许您从根本不存在的任何地址发送电子邮件!

For e-mail verification, well, most SMTPs have that option disabled so, you wouldn't be able to see whether an e-mail exist or not. 嗯,对于电子邮件验证,大多数SMTP都禁用了该选项,因此,您将无法查看电子邮件是否存在。

To do e-mail checking for real, you will have to consult POP server as well (in fact it goes first), to see whether there is anyone that can receive an e-mail on this address. 要进行真实的电子邮件检查,您还必须咨询POP服务器(实际上是第一个),以查看是否有人可以在该地址上接收电子邮件。

Here is the code to do checking only using SMTP (as you asked) by using smtplib: 这是使用smtplib仅使用SMTP(根据您的要求)进行检查的代码:



from smtplib import SMTP
from socket import timeout as ExcTimeout
import socket
import sys

addr2check = "something@somethingelse.woops"
# Note, if the server is subdomained for SMTP (like mail.somethingelse.woops) be sure to include it or server wouldn't be found!
#addr2check = "someone@mail.somewhere.here"

ok = 0
r = None
try:
    # For Python 2.5 and below (smtplib doesn't support timeout, so do it globally)
    if sys.version[:3]<="2.5":
        TO = socket.getdefaulttimeout()
        socket.setdefaulttimeout(8)
        try: s = SMTP(addr2check.split("@")[1])
        except: socket.setdefaulttimeout(TO); raise
        socket.setdefaulttimeout(TO)
    else: s = SMTP(addr2check.split("@")[1], timeout=8) # Maybe even bigger TO for some GSM and 56K modems
    ok += 1
    r = s.verify(addr2check)
    if r!=-1:
        ok += r[0]==250
    else:
        r = s.expn(addr2check)
        if r==-1: ok = -1
        else: ok += r[0]==250
    s.quit()
except ExcTimeout: print "Connection timed out! Server most probably doesn't exist!"
except Exception, e:
    # Here some checks for what exactly happened, or just:
    pass

msgs = {
    -1: "Server exists, but doesn't support e-mail verification!",
    0: "Server does not exist!!!",
    1: "Server exists but reports: "+str(r)+" for given e-mail!",
    2: "E-Mail "+addr2check+" most definitely exists! Confirmed by it's SMTP server!"
    }

print msgs[ok]

To extend your checking to POP3, use poplib! 要将检查扩展到POP3,请使用poplib! Algorithm will be similar to this one. 算法将与此类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM