Search code examples
phpemailverification

How to check whether an email id exists or not?


How to check whether an email id exists or not using PHP? and to get information about the owner of the email id? is it possible to get the information about the owner of the email id? do have to work with some protocols like POP? Please help me.


Solution

  • Lets say a user submits the following email address:

    • stackuser@stackoverflow.com

    The checks you would want to perform in order are like so:

    • Is the address valid
    • Does the domain run a mail server / MX Records
    • Is it blacklisted

    Firstly within PHP you can validate an email by using filter_var like so:

    $is_valid = filter_var("stackuser@stackoverflow.com",FILTER_VALIDATE_EMAIL);
    

    Secondly you would want to check if the domain runs a email server, to do this you can check the dns records for MX like so:

    $has_dns_mx_record = checkdnsrr("stackoverflow.com","MX");
    

    You might also want to open the port on the domain like so:

    $socket = fsockopen("stackoverflow.com", 25);
    $mail_running = (bool)$socket;
    fclose($socket);
    

    You can also check to see if the SMTP Server responds with a 550, i.e email does not exist, like so:

    SEND > helo hi
    250 stackoverflow.com
    
    SEND > mail from: <youremail@yoursite.com>
    250 2.1.0 Ok
    
    SEND > rcpt to: <stackuser@stackoverflow.com>
    > 550 5.1.1 <stackuser@stackoverflow.com>: Recipient address rejected: User unknown in local recipient table
    

    Looking at the above you can send commands to a valid smtp server such as helo > mail from <...> and check the 550 response.

    Take a look here for some response codes: http://www.greenend.org.uk/rjk/2000/05/21/smtp-replies.html

    Also you should take note of @slebetman's comment stating that a small percentage of mail > servers are configured to respond 550 to prevent the sniffing out of valid email addresses.

    The black list check is pretty simple, you would just find a decent DNSBL Server that provides a gateway for you check check the domain to see if it has been blacklisted, if it has the email may well be valid and active but has been marked as spam, therefore its an untrusted email and you should request an alternative email address to authorize against

    These are some of the validation techniques used to validate an email address, now there is plenty more validation methods but these are a few of the main ones.