Recently, one of our clients requested a functionality to parse and retrieve emails from his Gmail inbox using PHP that should be based on the email content. vteams engineer completed this request using Internet Message Access Protocol (IMAP) – an Internet standard protocol used by email clients to retrieve email messages from a mail server over a TCP/IP connection.
The example mentioned below will only work for retrieving emails from Gmail inbox if the following requirements are met:
- PHP version should be PHP5 (or the latest one)
- PHP IMAP Extension should be enabled in your PHP installation
- IMAP should be enabled in your Gmail settings
.
Enable IMAP in XAMPP
IMAP is not enabled by default in XAMPP distribution. To enable it, go to the file and search for . Now remove the beginning semicolon of . Once you are done, it gets enabled and now it should be like .
Enable IMAP in Linux
In Linux, you can install the PHP5 IMAP module with the following command:
It is also not enabled by default. You can do so using command. The changes can be seen after restarting Apache, using the following command:
PHP Script
Following is the PHP script for retrieving emails using IMAP:
Connect Email Servers with PHP imap_open
To connect email servers with , you need to follow the steps mentioned below:
- Connect to Gmail Inbox with IMAP Settings using this code:
- Connect to Yahoo Inbox with IMAP Settings using this code:
- Connect to AOL Inbox with IMAP Settings using this code:
- Connect to Local Mail Server using localhost IMAP Settings. Use the following code for it:
- To connect Custom Mail Server using IMAP Settings, you may need to connect to your company mail server at first using a function. You need to have correct imap server, ssl or tls enabled and a correct port. At times even if you provide the correct details, the server will generate a notification of certificate failure. In this situation, you have to use the option to allow the access to the email server.For connecting Custom Mail Server using IMAP Settings, use the following code: