Tuesday, April 27, 2010

SugarCRM Troubleshooting: IMAP Connectivity

You may have read my previous post from a few months ago where I discuss a potential scenario that may keep you from being able to use the SugarCRM email client for IMAP accounts that require SSL.

While troubleshooting similar connectivity issues, I have found it helpful to use some PHP scripts to perform the basics of verifying that the server is actually capable of connecting to the mail server.  

Assuming we cannot connect through the scripts, it would make sense that we would also not be able to connect via SugarCRM.  

Some of the common reasons for not being able to connect include:
  • Firewall restrictions
  • Missing PHP extensions (e.g. IMAP)
  • Incorrect account information 
Should any of those (or other) conditions exist, the scripts will fail and you know you need to look elsewhere for your solution, instead of within SugarCRM.

There are two scripts that make up this system.  The first should be named mail_config.php and contain the following code:


Original Dev: Angel Magaña, November 2009

Desc: Config file for mail connection

The contents of this file are governed by the GNU General Public License (GPL).
A copy of said license is available here:
This code is provided AS IS and WITHOUT WARRANTY OF ANY KIND.

$server = '{your.imap.server:993/service=imap/ssl/tls/validate-cert}INBOX';
$mail_user = '';
$mail_pass = 'your_password';


If you are not using SSL, change:




Next, update $mail_user and $mail_pass to reflect your IMAP account's credentials.

Lastly, create a second PHP file named test_imap.php and add the following code to it:


Original Dev: Angel Magaña, November 2009

Desc: Script for retrieving messages

The contents of this file are governed by the GNU General Public License (GPL).
A copy of said license is available here:
This code is provided AS IS and WITHOUT WARRANTY OF ANY KIND.

//Connect to E-mail inbox
$mbox = imap_open ($server, $mail_user, $mail_pass)
or die('Could not connect: ' . imap_last_error());

//Get msg count
$total_msgs = imap_num_msg($mbox);

echo 'Msg Total: ' . $total_msgs;

//Close inbox connection


To use the script and verify connectivity, place both PHP files into the same folder within your web server's documents/root folder and pull up the test_imap.php file within your browser. 

If all is fine, your browser will show you a total number of messages in the defined mailbox.  If not, an error will be returned.


  1. This was a very useful little script - thank you!

  2. seems very helpful, but what do you do if the browser just shows a blank page?

  3. @Kristian: Are you talking about a blank screen when you use this script or other circumstances?

    You should at minimum get an error message with this script if there is a problem.

  4. I got this error:

    Could not connect: Can't open mailbox {}INBOX: invalid remote specification

    The firewall is ok for inbound and outbound, php has the imap extension and the information account is also correct. So, I don't know what else try! I've been trying to fix this for 2 days...

    Any ideas? Thanks for you scripts!

  5. @Rosana:

    I am not familiar with that error, but I performed a quick search and a couple of similar situations seem to indicate it is a problem related to SSL on your server.

    See here:

  6. Thank Angel! I'll work on that! This is my first installation on Mac OS, so I might have to check something else.

  7. Hi Angel
    I got this error

    Could not connect: Can not authenticate to IMAP server: [AUTHORIZATIONFAILED] Account is blocked. Login to your account via a web browse

    And when I try connect from Sugar, sugar says me ""Connection failed to,993: Connection timed out" can you help me please?

    1. The error indicates that your email account is blocked. Sugar won't work until you correct that.


Your comments, feedback and suggestions are welcome, but please refrain from using offensive language and/or berating others. Thank you in advance.