Re: Help Reading from a POP3 Mail Server
| From: | Tim Streater | Date: | Fri, 07 Dec 2018 10:16:28 +0000 |
| Subject: | Re: Help Reading from a POP3 Mail Server | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-326630@lists.php.net to get a copy of this message | ||
On 07 Dec 2018, at 09:44, Sam Hobbs <Sam@SimpleSamples.info> wrote:
> Tim Streater wrote:
>> You need to rtrim() each input line, and stop when you find a line
>> consisting of just a full-stop (.)
>
> I think that RFC 1939 (Post Office Protocol - Version 3) and RFC 5322
> (Internet Message Format) are the relevant standards.
>
> RFC 5322 does show examples of commands being sent to the server and the
> responses sent from the server to the client and I do see lines with
> just a period but I don't see documentation of what lines like that mean.
The server will send out such a line when it has finished responding to a command which will
generate many lines of response, such as UIDL or RETR. After the last real response line, comes the
line just consisting of a . followed by a newline. That should be CR/LF but IME mail servers lie a
lot.
If a command has a line-line reply, then it won't send out the dot-line. The programmer has to
know which commands return one line, and which don't
There's also the complication of dot-stuffing, since nothing prevents a real data line
consisting of a . and nothing else. That is handled by prepending such a dot with another one (or
maybe it's any line starting with a dot, can't remember). I found that somewhere in the
RFCs.
I do this:
function networkRead ($fp, $where, &$line)
{
// Reads a line from the network and does dot-unstuffing.
$result = netRead ($fp, $line, $errno, $errstr, $where);
if ($result==false)
{
$msg = 'error in networkRead at ' . $where . ': ' . $errno . '
' . $errstr;
writeLog ($msg);
return READ_ERROR;
}
if ($line=='.') return READ_EOM;
if (strlen($line)>1 && $line[0]=='.') $line = substr ($line, 1);
return READ_OK;
}
where netRead (...) uses gets() to read a line and check for errors including timeout.
--
Cheers -- Tim