Email Code

  Home arrow Email Code arrow Checking if Email address is deliverab...

Checking if Email address is deliverable
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3

    Table of Contents:



    SMPT protocol check of email address at preferred MX host or host.

    This returns nothing if email is valid, or a nice descriptive error message. It will split out the domain, lookup the preferred mailhost, connect to it an try to deliver at the given address. If no MX record is found it will try to deliver directly at the SMTP port. Since most mailservers say that the given address has valid syntax the only more waterproof check would be to send mail and wait for a reply.

    This is the fullest implementation

    By : datasmid


    function checkemail($email){
    list($mailbox,$domain) = split('@',$email,2);
    $state = 'domain';

    // find preferred mailserver
    foreach($pref as $preferred){
    $mailserver = $mailhosts[key($pref)];
    $state = "trying mailserver $mailserver";
    $state = mailconnect($mailserver,$email);
    // no mail exchange found try as host
    $state = "No MX, trying $domain";
    $state = mailconnect($domain,$email);
    return $state;

    function mailconnect($mailserver,$email){
    $myhostname = $SERVER_NAME;
    $connection = fsockopen($mailserver, 25);
    $state = "connected to $mailserver";
    // Nothing to do with greeting
    //$smtpgreeting = fread($connection, 512);

    fputs($connection, "HELO $myhostname\r\n");
    $hello = fgets($connection, 512);
    $state = "chatting to $mailserver: $hello";
    fputs($connection, "MAIL FROM: <webserver@$myhostname>\r\n");
    $youok = fgets($connection, 512);
    $state = "chatting to $mailserver: $youok";
    fputs($connection, "RCPT TO: <$newaddress>\r\n");
    $recepient = fgets($connection, 512);
    $state = "chatting to $mailserver: $recepient";
    fputs($connection, "QUIT\r\n");
    $deliverable = true;
    $state = false;
    fputs($connection, "QUIT\r\n");
    $deliverable = true;
    $state = false;
    $deliverable = false;
    $state = "RCPT? $recepient $newaddress";
    $state = "$mailserver not accepting mail now, please try again.";
    //$state = 'mailserver not greeting me';
    $state = "$mailserver not listening";
    return $state;
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Email Code Articles
    More By Codewalkers

    blog comments powered by Disqus


    - Basic Ajax contact form
    - Random validation image
    - tinySendMail
    - SaferMail 0.7
    - Smtp Auth Email Script
    - Search Mime Email Structure
    - PHP Text / HTML Email with Attachments 2.1
    - Simple way to send mail wih one attached Doc...
    - Generic POP3 Class
    - PHP Text / HTML Email with Unlimited Attachm...
    - Another SendMail
    - email with attachment
    - Get Email Addresses from Strings
    - EmailCode
    - Email Validation - Gone Wild

    Developer Shed Affiliates


    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap