Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bug in PHP or my code? 1

Status
Not open for further replies.

jet042

MIS
Dec 23, 2002
258
US
As part of my job training, I'm writing an SMTP client. I'm running into a problem with socket_read(). From the manual:

Code:
Return Values

socket_read() returns the data as a string on success, or FALSE on error (including if the remote host has closed the connection). The error code can be retrieved with socket_last_error(). This code may be passed to socket_strerror() to get a textual representation of the error.

    Note: socket_read() returns a zero length string ("") when there is no more data to read.

Since after you send an EHLO the server can reply with a variable number of lines, I want to wait until it's done to send the MAIL FROM command. However, when I use the following code to read from the socket, I'm entering an endless loop because socket_read() never seems to return an empty string. Is this a bug in the function, or have I missed something in my own code?

Code:
foreach($mail_head as $in) {
  $output = "";
  $in .= "\r\n";
  echo $in;
  @socket_write($socket, $in, strlen($in)) or die("Error writing to socket: " . socket_strerror(socket_last_error()));
  do {
    $out = false;
    $out = socket_read($socket, 1024, PHP_BINARY_READ);
    $output .= $out;
  } while($out !== '');
  if(!preg_match('/^[23]/', $output)) {
    die("Error: Mail server did not return the expected response.");
  }
}

All of my SMTP commands, by the way, are stored in the mail_head array.
 
i suggest you have a look at the SMTP client within phpmailer for a good example of how to avoid this issue.
 
Once again, you know the right answer. Thanks, jpadie.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top