7

How to write a code to check an email is existing or not? For example donkey_sdh123@gmail.com, donkey_sdh123@yahoo.com, or donkey_sdh123@lycos.com all these emails do not exist in gmail, yahoo and lycos database.
enter image description here


See the screenshot. donkey_sdh123@gmail.com is bad. That means it does not exist. how can i implement the same thing in my project?

javascript, jquery shell script, c or c++ are welcome. Without .net.

Adil Malik
  • 6,279
  • 7
  • 48
  • 77
Sreeni Puthiyillam
  • 465
  • 2
  • 10
  • 26
  • Define precisely what you mean by "existing" and check for precisely that. – David Schwartz Jun 20 '12 at 13:15
  • 4
    "Existing" doesn't mean much, since a mail server is free to accept email to arbitrary addresses it's never heard of before. Short of sending a message to the address and waiting for a bounce, there's not much you can do. – Wooble Jun 20 '12 at 13:15
  • i have a text field : Email: spsps@gmail.com and when i click send email i need to check this is a living address or not – Sreeni Puthiyillam Jun 20 '12 at 13:16
  • 2
    You cannot check for that with much accuracy by any means. Even checking that a server will accept mail won't guarantee that a human being will ever read it, and certainly not that the *correct* human being will read it. The effort is a waste of time. Ensure that the email address is syntactically correct, and then if you need to make sure your user can read mail at that address, force a transaction involving a secret you send to the address. – Pointy Jun 20 '12 at 13:19
  • What does "is a living address" mean? You need to decide *precisely* what it is you are trying to do. – David Schwartz Jun 20 '12 at 13:24
  • see man urururruru4235r3@gmail.com , is a living address, you can send e-mail to this address ? if you send it will retrive with some information, the mail is not exist .!!!!!!!!!1 – Sreeni Puthiyillam Jun 20 '12 at 13:27
  • If you want to check what you get back when you send mail to an address, then send mail to that address and see what you get back. – David Schwartz Jun 22 '12 at 03:53
  • Does this answer your question? [How to check mail address is exists or not?](https://stackoverflow.com/questions/13514005/how-to-check-mail-address-is-exists-or-not) – tripleee Jan 24 '20 at 08:26

4 Answers4

7

In short: it is not possible. At most you can try to check if the domain in question have MX record and try to connect to its mail server. Even that won't guarantee it is in working condition though.

You absolutely can't check if a particular e-mail exists on it in some standartized way as there are many approaches to masquerade and aliasing that many server employ. Servers can and will report in SMTP exchange non-existent addresses as valid for many reasons in both VRFY and MAIL/RCPT. The only definite answer you can get is that e-mail is invalid if it is rejected by MAIL/RCPT, but being accepted is not definite proof of it being valid, as it can be rejected down the line of e-mail processing. Abusing MAIL/RCPT without actually sending anything can also lead to you being blocked.

If you want to verify user-supplied e-mail, your best bet is to send confirmation letter there.

You also should review if you really need confirmed working e-mail at all.

Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68
  • 1
    why not ? then how they did , please check the screen shot – Sreeni Puthiyillam Jun 20 '12 at 13:23
  • 2
    @Sreenivas: the fact that the service *claims* that it is able to do something doesn't mean that it is possible. There are snake-oil sellers. Though it depends on what do you mean by "e-mail exists". – jfs Jun 20 '12 at 13:34
  • 3
    Keywords: in some standartized way. For example GMail delivers mail to username+arbitrary_tag@gmail.com to username@gmail.com and some other services can do same, but not all of them will tell in response to `VRFY` that such address is valid. Another way - spamming `MAIL` and `RCPT` will likely to get you blocked. So confirmation letter is the only way to check both that address exists and it is **really address of intended user**. – Oleg V. Volkov Jun 20 '12 at 13:34
  • yes oleg, thats true, it so pathatic to write code with bash to get a actual result – Sreeni Puthiyillam Jun 20 '12 at 13:39
  • ok J.F. Sebastian i got what you said, yes i dont want to do in that way..if its not possible in legit way, fine,no issue – Sreeni Puthiyillam Jun 20 '12 at 13:40
2

It is possible to connect to a remote smtp server via telnet:

http://www.yuki-onna.co.uk/email/smtp.html

The only thing is that many mail servers will not accept smtp (e.g. yahoo), and since information about whether an address is valuable to spammers, making it easy to discover which email addresses exist is not in the interests of any email host.

You can do a basic check on whether the domain exists using ping or other network scans, and you could use the Google API to search for an email address in case it is listed on the internet in the clear. But it's you and all the scammers out there doing the same thing, so it's probably not worth trying.

Another thing to note is that many email providers permit users to provide derivative, temporary or alias addresses, which will work for some limited time or use, but which are not mailboxes themselves. In this circumstance even a checker that worked would think the address did not exist even though the user would receive an email sent to it.

Since expired addresses probably aren't released (to prevent people getting previous users' mail), there will then be sources of false positives (check says yes but is wrong), as well as the false negatives (check says no but is wrong), along with all the friction of being treated like a spammer.

Phil H
  • 19,928
  • 7
  • 68
  • 105
1

See this code i got it from web.

 class SmtpValidator {

    private $options = array(
            "port" => 25,
            "timeout" => 1,  // Connection timeout to remote mail server.
            "sender" => "info@webtrafficexchange.com",
            "short_response" => false,
    );

    /**
     *  Override the options for those specified.
     */
    function __construct($options = null) {
        if (!empty($options)) {
            if (is_array($options)) {
                foreach ($options as $key => $value) {
                    $this->options[$key] = $value;
                }
            }
        }
    }

    /**
     *  Validate the email address via SMTP.
     *  If 'shore_response' is true, the method will return true or false;
     *  Otherwise, the entire array of useful information will be provided.
     */
    public function validate($email, $options = null) {

        $result = array("valid" => false);
        $errors = array();

        // Email address (format) validation
        if (empty($email)) {
            $errors = array("Email address is required.\n");
        } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $errors = array("Invalid email address.\n");
        } else {
            list($username, $hostname) = split('@', $email);
            if (function_exists('getmxrr')) {
                if (getmxrr($hostname, $mxhosts, $mxweights)) {
                    $result['mx_records'] = array_combine($mxhosts, $mxweights);
                    asort($result['mx_records']);
                } else {
                    $errors = "No MX record found.";
                }
            }

            foreach ($mxhosts as $host) {
                $fp = @fsockopen($host, $this->options['port'], $errno, $errstr, 
                                       $this->options['timeout']);
                if ($fp) {
                    $data = fgets($fp);
                    $code = substr($data, 0, 3);
                    if($code == '220') {
                        $sender_domain = split('@', $this->options['sender']);
                        fwrite($fp, "HELO {$sender_domain}\r\n");
                        fread($fp, 4096);
                        fwrite($fp, "MAIL FROM: <{$this->options['sender']}>\r\n");
                        fgets($fp);
                        fwrite($fp, "RCPT TO:<{$email}>\r\n");
                        $data = fgets($fp);
                        $code = substr($data, 0, 3);
                        $result['response'] = array("code" => $code, "data" => $data);
                        fwrite($fp, "quit\r\n");
                        fclose($fp);
                        switch ($code) {
                            case "250":  // We're good, so exit out of foreach loop
                            case "421":  // Too many SMTP connections
                            case "450":
                            case "451":  // Graylisted
                            case "452":
                                $result['valid'] = true;
                                break 2;  // Assume 4xx return code is valid.
                            default:
                                $errors[] = "({$host}) RCPT TO: {$code}: {$data}\n";
                        }
                    } else {
                        $errors[] = "MTA Error: (Stream: {$data})\n";
                    }
                } else {
                    $errors[] = "{$errno}: $errstr\n";
                }
            }
        }
        if (!empty($errors)) {
            $result['errors'] = $errors;
        }
        return ($this->options['short_response']) ? $result['valid'] : $result;
    }
}
Sreeni Puthiyillam
  • 465
  • 2
  • 10
  • 26
  • 1
    I wouldn't use this code. it doesn't check for multiline smtp banners and just sends the HELO after the initial 220 line.. this would fail on many modern spamfilters with early-talker-detection (eg. postfix with postscreen enabled) – Gryphius Jun 21 '12 at 06:48
0

working with SMTP might not give you accurate results.

Scenario: If the mailbox exists but for security reasons the SMTP service is turned of by/from the email provider.

Issue: The Email Validator code using SMTP validation will return negative result even when the mailbox exists.

Solution: Use third party Email Validator services. Read more below: https://email-checker.net/validate

MannyDev
  • 1
  • 1