How to check if an email address exists without sending an email?

前端 未结 14 1991
半阙折子戏
半阙折子戏 2020-11-22 00:28

I have come across this PHP code to check email address using SMTP without sending an email.

Has anyone tried anything similar or does it work for you? Can you tell

14条回答
  •  眼角桃花
    2020-11-22 01:20

    Other answers here discuss the various problems with trying to do this. I thought I'd show how you might try this in case you wanted to learn by doing it yourself.

    You can connect to an mail server via telnet to ask whether an email address exists. Here's an example of testing an email address for stackoverflow.com:

    C:\>nslookup -q=mx stackoverflow.com
    Non-authoritative answer:
    stackoverflow.com       MX preference = 40, mail exchanger = STACKOVERFLOW.COM.S9B2.PSMTP.com
    stackoverflow.com       MX preference = 10, mail exchanger = STACKOVERFLOW.COM.S9A1.PSMTP.com
    stackoverflow.com       MX preference = 20, mail exchanger = STACKOVERFLOW.COM.S9A2.PSMTP.com
    stackoverflow.com       MX preference = 30, mail exchanger = STACKOVERFLOW.COM.S9B1.PSMTP.com
    
    C:\>telnet STACKOVERFLOW.COM.S9A1.PSMTP.com 25
    220 Postini ESMTP 213 y6_35_0c4 ready.  CA Business and Professions Code Section 17538.45 forbids use of this system for unsolicited electronic mail advertisements.
    
    helo hi
    250 Postini says hello back
    
    mail from: 
    250 Ok
    
    rcpt to: 
    550-5.1.1 The email account that you tried to reach does not exist. Please try
    550-5.1.1 double-checking the recipient's email address for typos or
    550-5.1.1 unnecessary spaces. Learn more at
    550 5.1.1 http://mail.google.com/support/bin/answer.py?answer=6596 w41si3198459wfd.71
    

    Lines prefixed with numeric codes are responses from the SMTP server. I added some blank lines to make it more readable.

    Many mail servers will not return this information as a means to prevent against email address harvesting by spammers, so you cannot rely on this technique. However you may have some success at cleaning out some obviously bad email addresses by detecting invalid mail servers, or having recipient addresses rejected as above.

    Note too that mail servers may blacklist you if you make too many requests of them.


    In PHP I believe you can use fsockopen, fwrite and fread to perform the above steps programmatically:

    $smtp_server = fsockopen("STACKOVERFLOW.COM.S9A1.PSMTP.com", 25, $errno, $errstr, 30);
    fwrite($smtp_server, "helo hi\r\n");
    fwrite($smtp_server, "mail from: \r\n");
    fwrite($smtp_server, "rcpt to: \r\n");
    

提交回复
热议问题