2
votes

I cannot retrieve my mail using imap functions but encounter an error saying 'Couldn't open stream'.

The message is
Warning: imap_open() [function.imap-open]: Couldn't open stream { ... :110 } on line 3

I printed the last error as well in my code,
Cannot connect to Mail: Connection failed to myserver.net,110: Connection timed out

I do not understand 'Connection timed out', because the POP3 server works fine with my mail client such as Outlook.

I followed the directions to retrieve my POP3 mails, and I confirmed through web search that I can use imap functions to retrieve mails in a POP3 server.

I also tried '/novalidate-cert' option and checked the username and password. But there was no difference.

I attached my code below. It is very mysterious for me, can I just connect to the POP3 server?

<?php
$mbox = imap_open("{mail.myserver.net:110/pop3}", "[email protected]", "passme") or die('LAST ERROR: Cannot connect to Mail: ' . imap_last_error());;

echo "<h1>Mailboxes</h1>\n";
$folders = imap_listmailbox($mbox, "{mail.myserver.net:110/pop3}", "*");

if ($folders == false) {
    echo "Call failed<br />\n";
} else {
    foreach ($folders as $val) {
        echo $val . "<br />\n";
    }
}

echo "<h1>Headers in INBOX</h1>\n";
$headers = imap_headers($mbox);

if ($headers == false) {
    echo "Call failed<br />\n";
} else {
    foreach ($headers as $val) {
        echo $val . "<br />\n";
    }
}

imap_close($mbox);
?>
1

1 Answers

2
votes

I would imagine your server is firewalled and not allowing outgoing connections, or specifically not allowing outgoing connections on port 110.

The credentials work in Outlook because you are probably trying that from somewhere other than your web host / server.

You can check on the server whether you can make outgoing connections or not:

<?php
    $test = @fsockopen("mail.example.com", 110, $errno, $errstr, 10);
    if (!$test)
    {
        echo "Nope! We're blocked by the server:<br /><pre>$errstr ($errno)</pre>\n";
    }
    else
    {
        echo "Woot! Our packets can escape! Run, packets, run!";
    }
?>