Strange SMTP problem

Posted on 2006-04-21
Last Modified: 2013-12-12
Ok, here's one that's got my mind wrapped up in a few knots.

I just wrote a script to check if an email address actually exists so that (after testing) I can implement it into my site's registration page & other mailer routines already in place on the site.

Here is the code for my script;

  $addr = $_GET['addr'];   //manually put into the url for testing purposes
  $parts = explode("@", $addr);
  $acct = $parts[0];
  $dom = $parts[1];
  $mail_from = "admin@mydomain.tld";

  // Check that MX(mail exchanger) record exists in domain check.
  // checkdnsrr function reference :
  if(checkdnsrr($dom, "MX"))
    echo "Confirmation : MX record for $dom exists.<br>";
    // If MX record exists, save MX record address.
    // getmxrr function reference :
    if(getmxrr($dom, $MXHost))
      echo "Confirmation : Confirming address by MX LOOKUP.<br>";
      for ( $i = 0,$j = 1; $i < count ( $MXHost ); $i++,$j++ )
        echo "Result($j) - " . $MXHost[$i] . "<BR>";

    $sock = fsockopen($MXHost[0], 25, $errno, $errstr, 30);
      print "Connection established to $dom on server " . $MXHost[0] . "<br>\n";
      if(socket_set_blocking($sock, true))
        print "Blocking mode enabled<br>";
        print "<p>Sending: HELO " . $_SERVER['HTTP_HOST'] . "<br>";
        fputs($sock, "HELO " . $_SERVER['HTTP_HOST'] . "\r\n");
        print fgets($sock, 1024) . "<br>"; // Display server's response.

        print "Sending: MAIL FROM: &lt;$mail_from&gt;<br>";
        fputs($sock, "MAIL FROM: <$mail_from>\r\n");
        print fgets($sock, 1024) . "<br>"; // Display server's response.

        print "Sending: RCPT TO: &lt;$addr&gt;<br>";
        fputs($sock, "RCPT TO: <$addr>\r\n");
        print fgets($sock, 1024) . "<br>"; // Display server's response.

        print "Sending: QUIT<br>";
        fputs($sock, "QUIT\r\n");
        print fgets($sock, 1024) . "<br>"; // Display server's response.
        print "Blocking mode unable to be set<br>";
      print "Connection failed to $dom<br>\n";
    print (fclose($sock)) ? "Socket successfully closed<br>" : "Socket failed to close<br>";

Ok, here's the strange part now...the script actually works great on 95% of the all mail servers I tried. Testing was done using actual addresses from our site's member table, so the addresses are confirmed as good already, but when I try testing only certain addresses (the one below was one that failed the test with those results), I keep getting the following error message returned back from the mailserver in question;

Sending: HELO www.mydomain.tld
503 5.5.0 : Client host rejected: Improper use of SMTP command pipelining
Sending: MAIL FROM: <admin@mydomain.tld>
503 5.7.0 Error: access denied for[]
Sending: RCPT TO: <>
503 5.7.0 Error: access denied for[]
Sending: QUIT
503 5.7.0 Error: access denied for[]
Socket successfully closed

Yet if I check a Yahoo or Hotmail address or just about most other mailservers, I get a nice set of 250 responses back showing that each command was accepted just fine.

Not quite sure how to troubleshoot this one, or perhaps this is totally on the remote mailserver's side and there's nothing I can do. Any ideas would be of great help.

Thanks everyone, looking forward to what becomes of this :)
Question by:adrian_brooks
    LVL 4

    Accepted Solution

    SMTP command pipelining is an addition to the standard; most servers don't really care about this, but it's one of the first filters against spam (apparantly, check for the exact explanation).

    I do not exactly know however, how you could solve this fast.
    a good start however, could be looking at ones the php smtp classes, like for example this one:
    the author notes that you can use SMTP command pipelining with this class, so I think a quick thrawl through this class might give you all you need to know to get it working.

    hope it helps,

    LVL 12

    Author Comment

    Hi Andy,

    First, thanks for the response.

    I'll look into that freshmeat class. For the time being, I'm primarily interested in only running a basic test via SMTP to just authenticate that a submitted email account is, in fact, real. I'm trying to work on a way of checking addresses for validity as well as being able to notifiy the client of a delivery failure when they try to send an ecard. Currently, all emails are 100% originated by my server, so the client has not true direct access to my SMTP server, therefore, they have no direct connection to any mail delivery responses I receive as a result of their card not having made it to it's destination.

    Also, during the registration phase, I am hoping to use this method as a way of pre-qualifying the client to ensure that when the server sends them their account activation mailer, that it is going to a valid address. I guess this would just be a more anal level of form validation.

    Anyway, you get the idea behind what I am trying to acheive. My current mailer class seems to work fine, so I don't have a need to replace it with another class, just pggy back everything with a good SMTP communication handler. So, I'll have a peek at that one you submitted and let you know what I found. :)

    Thanks again, Andy

    LVL 12

    Author Comment

    It would appear that I am going to need to really dig into this one. I'm guessing that I am going to have to really study SMTP command pipelining to better understand how it is that I could be causing their mailservers to spit back those responses. This one is definitely going to be a real brainwracking issue I can see.

    I'm going to award you the point though for having at least given this a fair try at resolving it.

    Take care & thanks again, Andy

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now