[Solved] Fatal error: Call to undefined function imap_open() in PHP

I am trying to access my gmail account through my localhost. However, I am getting the response:

Fatal error: Call to undefined function imap_open()

This is my code:

$hostname = '{}INBOX';
$username = '[email protected]';
$password = 'mypassword';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' .imap_last_error());
Enquirer: Sumit


Solution #1:

Simple enough, the IMAP extension is not activated in your PHP installation. It is not enabled by default. If your local installation is running XAMPP on Windows, you have to enable it as described in the XAMPP FAQ:

Where is the IMAP support for PHP?

As default, the IMAP support for PHP is deactivated in XAMPP, because
there were some mysterious initialization errors with some home
versions like Windows 98. Who works with NT systems, can open the file
"xamppphpphp.ini" to active the php exstension by removing the
beginning semicolon at the line ";extension=php_imap.dll". Should be:

Now restart Apache and IMAP should work. You can use the same steps
for every extension, which is not enabled in the default

Respondent: Lekensteyn

Solution #2:

The Installation Procedure is always the same, but the package-manager and package-name varies, depending which distribution, version and/or repository one uses. In general, the steps are:

a) at first, user privilege escalation is required, either obtained with the commands su or sudo.

b) then one can install the absent PHP module with a package manager.

c) after that, restarting the apache2 HTTP daemon is required to load the module.

d) at last, one can run php -m | grep imap to see if the PHP module is now available.

On Ubuntu the APT package php5-imap (or php-imap) can bei installed with apt-get:

apt-get install php5-imap
service apache2 restart

On Debian, the APT package php5-imap can be installed aptitude (or apt-get):

aptitude install php5-imap
apache2ctl graceful

On CentOS and Fedora the RPM package php-imap can be installed with yum (hint: the name of the package might be something alike php56w-imap or php71w-imap, when using Webtatic repo):

yum install php-imap
service httpd restart

On systemd systems, while using systemd units, the command to restart unit httpd.service is:

systemctl restart httpd.service

The solution stated above has the problem, that when the module was already referenced in:


It might throw a:

PHP Warning:  Module 'imap' already loaded in Unknown on line 0

That happens, because it is referenced in the default php.ini file (at least on Ubuntu 12.04) and a PHP module must at most be referenced once. Using INI snippets to load modules is suggested, while the the directory /etc/php5/conf.d/ (that path may also vary) is being scanned for INI files:


Ubuntu also features proprietary commands to manage PHP modules, to be executed before restarting the web-server:

php5enmod imap
php5dismod imap

Once the IMAP module is loaded into the server, the PHP IMAP Functions should then become available; best practice may be, to check if a module is even loaded, before attempting to utilize it.

Respondent: Martin Zeitler

Solution #3:

In Ubuntu for install imap use

sudo apt-get install php-imap

Ubuntu 14.04 and above use

sudo apt-get install php5-imap

And imap by default not enabled by PHP so use this command to enable imap extension

sudo php5enmod imap

Then restart your Apache

sudo service apache2 restart
Respondent: pramod kadam

Solution #4:

if you are on linux, edit the /etc/php/php.ini (or you will have to create a new extension import file at /etc/php5/cli/conf.d) file so that you add the imap shared object file and then, restart the apache server. Uncomment


so that it becomes like this:

Then, restart the apache by

# /etc/rc.d/httpd restart
Respondent: Shiva Nandan

Solution #5:


echo get_cfg_var('cfg_file_path');

you can find out which php.ini has been used by this instance of php.

Respondent: abhilashv

Solution #6:

During migration from Ubuntu 12.04 to 14.04 I stumbled over this as well and wanted to share that as of Ubuntu 14.04 LTS the IMAP extension seems no longer to be loaded per default.

Check to verify if the extension is installed:

dpkg -l | grep php5-imap

should give a response like this:

ii  php5-imap       5.4.6-0ubuntu5   amd64        IMAP module for php5

if not, install it.

To actually enable the extension

cd /etc/php5/apache2/conf.d
ln -s ../../mods-available/imap.ini 20-imap.ini
service apache2 restart

should fix it for apache. For CLI do the same in /etc/php5/cli/conf.d

Respondent: M5M400

Solution #7:

On Mac OS X with Homebrew, as obviously, PHP is already installed due to provided error we cannot run:

Update: Tha latest version brew instal php --with-imap will not work any more!!!

$ brew install php72 --with-imap
Warning: homebrew/php/php72 is already installed

Also, installing module only, here will not work:

$ brew install php72-imap
Error: No available formula with the name "php72-imap"

So, we must reinstall it:

$ brew reinstall php72 --with-imap

It will take a while 🙂 (built in 8 minutes 17 seconds)

Respondent: Vladimir Vukanac

Solution #8:

if it is centos with php 5.3 installed.

sudo yum install php53-imap

and restart apache

sudo /sbin/service httpd restart


sudo service apache2 restart
Respondent: Robert

Solution #9:

I had the same issue. After changing a semicolon in the php.ini file I solved my problem. Let’s see how can you solve it?

First, open php.ini file. xampp/php/php.ini

Search extension=imap

remove semicolon before extension and save this file.

Code before remove semicolon ;extension=imap

Code after remove semicolon extension=imap

Finally, open the XAMPP Control panel and restart Apache.

Respondent: Sumit Kumar Gupta

Solution #10:

Ubuntu with Nginx and PHP-FPM 7 use this:

sudo apt-get install php-imap

service php7.0-fpm restart
service ngnix restart

check the module have been installed
php -m | grep imap

Configuration for module imap will be enabled automatically, both at cli php.ini and at fpm php.ini

nano /etc/php/7.0/cli/conf.d/20-imap.ini
nano /etc/php/7.0/fpm/conf.d/20-imap.ini

Solution #11:

If your local installation is running XAMPP on Windows , That’s enough :
you can open the file “xamppphpphp.ini” to activate the php exstension by removing the beginning semicolon at the line “;extension=php_imap.dll”. It should be:



Respondent: Mahdi Bashirpour

Solution #12:

To install IMAP on PHP 7.0.32 on Ubuntu 16.04. Go to the given link and based on your area select link. In my case, I select a link from the Asia section. Then a file will be downloaded. just click on the file to install IMAP .Then restart apache

to check if IMAP is installed check phpinfo file.incase of successful installation
IMAP c-Client Version 2007f will be shown.

Respondent: Amal

Solution #13:

For Xampp 7.3.9

PHP.ini line 913:


You should remove the semicolon.

Respondent: Carlos Servin

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy