Pages

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:

<?php

/*************************************
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: http://www.gnu.org/copyleft/gpl.html
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 = 'your_acct@gmail.com';
$mail_pass = 'your_password';

?>

If you are not using SSL, change:

{your.imap.server:993/service=imap/ssl/tls/validate-cert}INBOX

to

{your.imap.server:143/service=imap}INBOX

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:

<?php

/*************************************
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: http://www.gnu.org/copyleft/gpl.html
This code is provided AS IS and WITHOUT WARRANTY OF ANY KIND.
*************************************/

//Connect to E-mail inbox
require_once('mail_config.php');
$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
imap_close($mbox);

?>

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.

8 comments:

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

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

    ReplyDelete
  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.

    ReplyDelete
  4. I got this error:

    Could not connect: Can't open mailbox {imap.gmail.com:993/service=imap/ssl/tls/validate-cert}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!

    ReplyDelete
  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:

    http://stackoverflow.com/questions/6727276/php-imap-open-invalid-remote-specification-when-trying-connect-to-gmail

    http://www.phpbuilder.com/board/archive/index.php/t-10187020.html

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

    ReplyDelete
  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 pod51017.outlook.com,993: Connection timed out" can you help me please?

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

      Delete

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