As part of my job training, I'm writing an SMTP client. I'm running into a problem with socket_read(). From the manual:
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?
All of my SMTP commands, by the way, are stored in the mail_head array.
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.