title: Debugging php imap_open function - possible networking error
body: <p><strong>BACKGROUND:</strong> I just migrated my project to a different server (a shared DigitalOcean one, the cheapest offer) with CentOS, and noticed that PHP's imap functions take way more on the new server, than on the old one(like 10x more).</p>
  
  <p>The old server is a dedicated server, and a mail server is also hosted on the old server, the one which I am trying to perform imap actions on. </p>
  
  <p><strong>IMPORTANT:</strong> I don't think that this slow down has anything to do with not trying to connect from the same physical server to the mail server anymore, because of the severe increase of the time it requires to run any imap function throughout my project, but anyone can prove me wrong, because I don't know anything about networking unfortunatly:(</p>
  
  <p>I've made some simple script runtime test on both servers, by creating two php files, and testing the execution time of both, one using only php's imap functions (imap_open), and another one, where I am using PEAR Net_IMAP package. Here are the two scripts:</p>
  
  <p><strong>SCRIPT 1:</strong></p>
  
  <pre><code>$start=microtime(true);
  $temp=$start;
  $mbox = imap_open ("{mymailserver:143/novalidate-cert}INBOX", "email@address.com", "password");
  telltime($start, "Connect and Login", $temp);
  $mbox = imap_reopen ($mbox,"{mymailserver:143/novalidate-cert}INBOX.Sent");
  telltime($start, "Changefolder", $temp);
  telltime($start, "End script");
  </code></pre>
  
  <p><strong>SCRIPT 2:</strong></p>
  
  <pre><code>require_once 'Net/IMAP.php'; 
  $imapServer = new Net_IMAP('mymailserver', 143);
  telltime($start, "Connect", $temp);
  $loggedIn = $imapServer-&gt;login('email@address.com' , 'password'); 
  telltime($start, "Login", $temp);
  $imapServer-&gt;selectMailbox("INBOX.Sent");
  telltime($start, "Change folder", $temp);
  telltime($start, "End script");
  </code></pre>
  
  <p>I've ran these scripts the following way, with the following results:</p>
  
  <p><strong>SCRIPT 1 AS IS - old server</strong></p>
  
  <blockquote>
    <p>Connect and Login: 0.124350070953<BR>
    Changefolder: 0.00585293769836<BR>
    Whole time: 0.130313158035<BR></p>
  </blockquote>
  
  <p><strong>SCRIPT 1 AS IS - new server</strong></p>
  
  <blockquote>
    <p>Connect and Login: 0.63277888298035 <BR>
    Changefolder: 0.15838479995728 <BR>
    Whole time: 0.79174709320068 <BR></p>
  </blockquote>
  
  <p><strong>SCRIPT 1 /novalidate-cert changed to /notls - old server</strong></p>
  
  <blockquote>
    <p>Connect and Login: 0.112071990967 <br>
    Changefolder: 0.00407910346985 <BR>
    Whole time: 0.116246938705 <BR></p>
  </blockquote>
  
  <p><strong>SCRIPT 1 /novalidate-cert changed to /notls - new server</strong></p>
  
  <blockquote>
    <p>Connect and Login: 0.50686407089233 <BR>
    Changefolder: 0.17428183555603 <BR>
    Whole time: 0.68127012252808 <BR></p>
  </blockquote>
  
  <p><strong>SCRIPT 2 AS IS - new server</strong></p>
  
  <blockquote>
    <p>Connect: 0.42295503616333 <BR>
    Login: 0.4013729095459 <BR>
    Change folder: 0.057337045669556 <BR>
    End script: 0.88185501098633 <BR></p>
  </blockquote>
  
  <p>The project also has a console based debugging system, from which I've managed to gather the following information:<BR>
   <em>- an average search to the mailbox takes around 0.01-0.02 seconds on the old server, while on the new server, the same search takes around 7-8x times more</em><BR>
   <em>- the fetching of a single e-mail from the mail server on the old server takes between 0.05s-0.1s while on the new server, there are e-mails(mainly those which have Text/HTML with attached image files) which take <strong>4</strong> seconds to fetch</em></p>
  
  <p>Based on these results, I am presuming that I am facing a networking problem, but it is just a wild assumption, as till now I've never debugged networking errors, but I already took apart my php scripts, and I can't find any error in them.</p>
  
  <p>I tried <strong>traceroute</strong>-ing and <strong>ping</strong>-ing the mail server from the new project environment, and I've got the following results:</p>
  
  <pre><code>traceroute to mymailserver (xxx.xxx.xxx.xx), 30 hops max, 60 byte packets
  1  xxx.xxx.x.xxx 0.658 ms xxx.xxx.x.xxx 0.510 ms xxx.xxx.x.xxx 0.471 ms
  2  xxx.xxx.xxx.xxx  0.434 ms xxx.xxx.xxx.xxx 0.333 ms xxx.xxx.xxx.xxx 0.247 ms
  3  xxx.xxx.xxx.xx 0.984 ms  0.986 ms xxx.xxx.xxx.xxx 0.270 ms
  4  xxx.xxx.xxx.xx 0.964 ms xxx.xxx.xx.xxx 1.414 ms  1.449 ms
  5  xxx.xxx.xx.xxx 1.253 ms  1.211 ms xxx.xxx.xx.xxx 22.078 ms
  6  xxx.xxx.xx.xxx 43.920 ms  41.971 ms  44.860 ms
  7  xx.xx.xx.xxx 45.835 ms xxx.xxx.xx.xxx 42.055 ms  41.254 ms
  8  * xxx.xxx.xxx.xxx 42.999 ms *
  9  xxx.xxx.xxx.xx 41.989 ms  42.235 ms  44.925 ms
  </code></pre>
  
  <p>Yes, sometimes traceroute reports about lost packages, but not always, but the rest of this information unfortunatly is only giberish to me, cause I don't understand for what I have to look for, and I didn't find any usable tutorial for traceroute on the internet.</p>
  
  <p>** OTHER INFORMATION BASED ON THE HELP FROM OTHER STACKOVERFLOW USERS: **<BR>
  I've also downloaded xDebug, and tried function tracing the two above mentioned scripts. While I didn't get any usefull information by function tracing imap_open function, I've noticed that when I function trace the PEAR package, the fgets() that are performed somewhere in the class, take considerably more time, than any other function that is being run(around 0.05 seconds)</p>
  
  <p><strong>My question(s):</strong><br>
   - Am I right in assuming from these information that this has to be a networking problem?<br>
   - If yes, how could I solve it, or what are the giveaways that this is indeed a networking problem?<BR>
   - If no, how could I isolate the problem better, and obtain a solution to it?</p>
  
  <p>I am offering a:<BR>
  - a thank you, to someone who helps me isolate if this is a networking problem, or something else<BR>
  - a bounty, if someone manages to help me solve this issue, and speed up the processing of imap functions</p>