Re: Help Reading from a POP3 Mail Server
- Date: Fri, 07 Dec 2018 10:16:28 +0000
- From: Tim Streater <tim@xxxxxxxxxxxxxxxx>
- Subject: Re: Help Reading from a POP3 Mail Server
On 07 Dec 2018, at 09:44, Sam Hobbs <Sam@xxxxxxxxxxxxxxxxxx> 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);
$msg = 'error in networkRead at ' . $where . ': ' . $errno . ' ' . $errstr;
if ($line=='.') return READ_EOM;
if (strlen($line)>1 && $line=='.') $line = substr ($line, 1);
where netRead (...) uses gets() to read a line and check for errors including timeout.
Cheers -- Tim