Web lists-archives.com

Re: Help Reading from a POP3 Mail Server




On 07 Dec 2018, at 01:56, John <john.iliffe@xxxxxxxxx> wrote:


> I am trying to get e-mails one at a time from a pop3 mail server. I can open

> the pop3 socket on port 110 using

>

> fsockopen(tcp://127.0.0.1, 110, $errno, $errstr)

>

> and I have no problem logging on to the mail account and issuing the LIST and

> RETR commands.

>

> BUT getting the actual email has me stumped. Using fgets() works OK IF I can

> tell it how many lines to expect. If not, it runs off the end of the file and

> I

> have to cancel the job manually. If I use the code in the fread()

> documentation

> it does the same thing:

>

> $message = "";

> while (! feof($mail_sock))

> {

> $message .= fread($mail_sock, 8192);

> }

>

> hangs at the end of the email and I need to cancel manually. Putting an echo

> command right after the fread() statement shows that the entire message

> arrives

> correctly before it hangs.

>

> I have tried checking for the ".\r\n" standard pop3 EOF indicator and breaking

> out of the while loop when I find it but that is unstable, that combination

> occurred during testing in a random email. Same thing happened using fgets()

> before I started trying fread().


You need to rtrim() each input line, and stop when you find a line consisting of just a full-stop (.)


Also, use fgets() to get the lines. Much easier.


$message = '';

while (true)

{

$line = rtrim (fgets ($mail_sock);

if ($line=='.') break; // (Well, I think it's "break;" I should use to exit this loop :-)

$message .= $line;

}


I've had no problems doing this. See here:


<http://www.iletter.org.uk>


for a practical example (you want v2, not v2).



--
Cheers -- Tim