Trying to connect to email server from php script using imap_open

487 Views Asked by At

I'm not a programmer, so please bear with me. Sorry if this is dumb.

I can create and use php scripts on my website no problem. For the last few days, I've been trying to figure out a way to check my email programmatically. The most common solution seemed to involve connecting using imap_open. Here is an example of just that one line of code:

$mail = imap_open("{mymailserver:110}INBOX", "[email protected]", "mypassword");

And obviously my mail server, username, and password go between the quotes. Just in that one opening line, the page hangs and eventually gives a 504 Gateway Time-out error.

Thunderbird can connect to my email using the same mail server, port, name and password. If i plug in info from a gmail account, it still hangs and times out. When i run phpinfo it says something about imap enabled (sorry if that's not relevant, i'm not sure).

Network Solutions, who hosts my site, told me that I shouldn't be using scripts to connect to email, but they would look into it if I paid a fee. Any help would be greatly appreciated, as I've been trying this for 20 hrs. Thanks.

1

There are 1 best solutions below

5
Ken Lee On

Port 110 is for POP3. I suggest you try to connect thru port 143 (IMAP)

Please try using the following codes to test (substitute imap.example.com by your mail server say mail.test.com) :

Note: for the username, in your case it may be your email address ([email protected])

<?php

ini_set('max_execution_time', 300); //300 seconds = 5 minutes 


$mbox = imap_open("{imap.example.com:143}", "username", "password");

echo "<h1>Mailboxes</h1>\n";
$folders = imap_listmailbox($mbox, "{imap.example.com:143}", "*");

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);
?>