Monday, 17 September 2018

Using PHP IMAP functions to download email from Gmail

A couple of weeks back I posted how to use the PHP IMAP functions to download email from an IMAP server. This post looks at how to do the same but downloading IMAP mail from Gmail, which you need to connect to on a different port and use SSL. Earlier today I posted how to enable IMAP mail access in Gmail so you'll need to ensure that's done first.

How to connect to a normal IMAP server

As a quick recap, this is how you connect to an IMAP mail server using PHP, where $server is the name of the IMAP mail server (including {curly brackets} :ports and /flags), $login is your login name and $password is your password:
$connection = imap_open('$server', $login, $password);

How to connect to Gmail's IMAP server

Gmail requires the IMAP connection to be made on port 993 and it must be an SSL connection.The hostname is imap.gmail.com. Therefore, to connect to Gmail IMAP with the PHP IMAP functions you would do this:
$server = '{imap.gmail.com:993/ssl}';
$connection = imap_open($server, $login, $password);
Note that your login name for Gmail includes the domain name as well.
If you have an @gmail.com email address then your login name would be e.g. 'my-email-address@gmail.com'
If you use Google Apps and have your domain mail hosted with Gmail then it will be your account email address e.g. 'chris@example.com'
IMAP supports the concept of multiple mailboxes. In Gmail your labels become mailboxes in your IMAP connection. When you first connect using the above you'll be looking at the INBOX. It is possible to then change to one of your other mailboxes and read mail from there.

How to download the email using IMAP from Gmail

At this stage it's the same as downloading mail from any IMAP server, so refer to my other post which shows how to use the PHP IMAP functions to download email from an IMAP server


Update - novalidate-cert flag

I recently moved from CentOS 5 with PHP 5.1.6 to Debian 5 with PHP 5.2.6 in my development environment (and will also do in production next week). My same test script would no longer connect to Gmail, instead error'ing out with:
Warning: imap_open(): Couldn't open stream {imap.gmail.com:993/ssl}
After various testing of different flags I discovered I needed to add the 'novalidate-cert' flag so the server string looks like this:
$server = '{imap.gmail.com:993/ssl/novalidate-cert}';
The next post in this series will look at how to open other mailboxes with PHP IMAP, using Gmail's mailboxes and labels as an example.

Update - firewall issues

I received an email from Jesse Fisher who was having issues connecting to Gmail with the PHP IMAP functions and was getting the error message "Can't connect to gmail-imap.l.google.com,993: Connection refused".
It turned out that port 993 was blocked by the firewall on the server that the PHP script was running on. Once the hosting provider opened that port the issue went away and Jesse was able to connect.
If you are getting an error like this connecting to a mail server using the PHP IMAP functions then checking the outbound firewall settings is one thing that needs to be looked at.
 

Related posts:

0 comments:

Post a Comment