Reputation: 45
I am using the script from David Walsh which connects to Gmail via IMAP and outputs the email data on-screen.
I have run two test cases:
The times for reading 120 emails are very different. For Gmail, the whole script takes about 5secs, with 1.2 connecting and 3.8 reading the emails and 0.1629 connecting and 0.0238.
These values are very different to what I would have expected.
What I have done so far:
I've tried calling the server in two ways:
In both cases it worked just the same, it was very slow.
Can anybody help me?
LATER EDIT:
Some folks ask if I have used Gmail API. Yes I have used it, and it's slower than the Google IMAP solution. I need to scan my Google mailbox, with IMAP, every 1 second. I know that it can be done, because I am copying another app, which I know for sure is doing this.
Upvotes: 4
Views: 4253
Reputation: 1
Check ping time to imap.gmail.com --- for me it's > 100ms, so it's not surprising that imap is slow.
Upvotes: -1
Reputation: 2950
From my experience, for this to work in some useable way, you need to first take a look at how Web Mail platforms work. When emails are viewed on page, the providers do not pull all emails from the email server on login to the page. If this was the norm, then servers/disks would struggle with load all the time. The queries are controlled and the usually show around 50-100 items at one time on screen. Some even give the appearance that all items are showing (Outlook Web Access) but in fact they are kicking off a search when the user scrolls. I recommend that you test with my script below which limits the amount of results for the fetch query. It was built for testing a similar issue and has been great for testing. Change $mailNumber
to the amount of records you would like to print on screen (INT
) and test. Each email can be selected and you should see the view the plain text (unless email was only wrote in HTML). I've also disabled SSL Validation as this can also slow down response times on connection.
$imapServ = "imap.server.com";
$imapPort = "993";
$imapUser = "EMAIL";
$imapPass = "PASSWORD";
$mbox = imap_open("{" . $imapServ . ":" . $imapPort . "/imap/ssl/novalidate-cert}INBOX", $imapUser, $imapPass);
if (isset($_GET['email'])) {
$result = imap_fetchbody($mbox, $_GET['email'], 1);
echo "<p>$result</p>";
echo "<br>";
echo "<b><a href=\"" . $_SERVER['SCRIPT_NAME'] . "\">Back To List</a></b>";
} else {
$mc = imap_check($mbox); //Total count of mail in inbox
$mailNumber = $mc->Nmsgs / 20; //Set Number for Email List Here
$result = imap_fetch_overview($mbox,"1:" . round($mailNumber) . "",0);
foreach ($result as $v) {
echo "<a href=\"" . $_SERVER['SCRIPT_NAME'] . "?email=" . $v->uid . "\"><b>From:</b>" . $v->from . " <b>Subject: </b>" . $v->subject . " <b>Date: </b>" . $v->date . "</a>";
echo "<br>";
}
}
Response to comment:
As this only happens with Gmail, and I'm guessing you have a stable internet connection, I suspect the issue is down to Gmail throttling bandwidth on external IMAP connections. To prove this, test it on other providers and investigate the results. Don't forget most people either use the Gmail portal (no doubt directly connected to an un-throttled IMAP data server direct) and email client which caches IMAP data so they would only be checking in for new mail and then storing mail on client hence why these symptoms would not be noticeable.
It may also be worth considering a DB solution to store IMAP data and then frequently compare this with Gmail IMAP server. This way your only bottleneck is your DB. Baring that, you would need to raise this with Google direct but I doubt they would offer much assistance for a free service.
Your last option would be to use a completely different solution. Google has a Gmail API so you could see if this is any quicker pulling data from the given mailbox.
Further Comment Response:
As you've loosely mentioned, whether you use the API or IMAP, your accessing a service via a protocol which you have no real control off with regards to speed if your code has been optimised. The example above removes the Javascript/HTML bloatware for testing. As this has shown no real speed improvements and you've confirmed that IMAP works drastically quicker on your hosted platform, the issue lies with Gmail or your ISP so you will need to liaise directly with them if you have any chances of resolving. I highly doubt it's your ISP but it's still a point of call if you get no where with Google. I would of suggested DNS changes but I can see that you have already made the relevant changes in hopes to resolve (especially testing via IP).
Upvotes: 2