67

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 = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'myid@gmail.com';
$password = 'mypassword';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' .imap_last_error());
double-beep
  • 5,031
  • 17
  • 33
  • 41
Sumit
  • 2,242
  • 4
  • 25
  • 43

13 Answers13

98

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 "\xampp\php\php.ini" to active the php exstension by removing the beginning semicolon at the line ";extension=php_imap.dll". Should be: extension=php_imap.dll

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

Rob W
  • 341,306
  • 83
  • 791
  • 678
Lekensteyn
  • 64,486
  • 22
  • 159
  • 192
  • followed your instructions but still getting error Fatal error: Call to undefined function imap_open() in – Sunil Bhawsar Jul 05 '16 at 06:50
  • @SunilBhawsar Ensure that PHP is compiled with IMAP support and that either it is a built-in or that the extension file is available in the extension directory. Be sure to check the error logs as well. – Lekensteyn Jul 05 '16 at 08:42
  • Warning: imap_header(): Bad message number in.. Failed to retrieve headers....... after that can you please help me short it out – Sunil Bhawsar Jul 05 '16 at 09:24
  • Can you please check [this] Need help (http://stackoverflow.com/questions/39745784/how-to-fetch-to-and-cc-email-using-imap-php) – Ayaz Ali Shah Sep 28 '16 at 11:18
65

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:

/etc/php5/apache2/php.ini

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:

/etc/php5/conf.d/imap.ini

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.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • On Ubuntu I had to execute "php5enmod imap" in addition to installing the package! - see anser of @pramod kadam below – mattanja May 26 '16 at 10:34
  • @mattanja it depends if `conf.d/imap.ini` is being present and parsed, whether or not, while `php5enmod` is a Ubuntu specific command, which basically does nothing else than to create that file - while `php5dismod` deletes it. updated the answer, according to your suggestion. – Martin Zeitler May 26 '16 at 15:13
  • understood. Just sayin, the answer is more complete now, thank you! (Providing the info right in the description for Ubuntu might save another few seconds for someone ;) – mattanja May 27 '16 at 08:30
  • 1
    For PHP7 it's _apt-get install php-imap_ (Ubuntu) – Felix May 02 '17 at 09:35
39

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
double-beep
  • 5,031
  • 17
  • 33
  • 41
pramod kadam
  • 1,287
  • 11
  • 11
  • +1 it was my case :or you can go to /etc/php5/apache2/conf.d and do a ln -s ../../mods-available/imap.ini ./20-imap.ini – Mike Castro Demaria Sep 14 '14 at 10:12
  • 3
    Good call on the `php5enmod` line, wasn't in the other answers but it didn't work for me until I'd done that. – M1ke Nov 27 '14 at 15:02
  • 1
    regarding http://www.php.net/manual/en/imap.setup.php on Ubuntu 14.04+ it should be sudo apt-get install php5-imap (not php-imap) sudo php5enmod imap – lubart Sep 02 '15 at 10:10
  • 1
    To enable imap for php7 you can run simply "sudo phpenmod imap" – Yes Kay Selva Dec 21 '16 at 07:29
7

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

;extension=imap.so

so that it becomes like this:

extension=imap.so

Then, restart the apache by

# /etc/rc.d/httpd restart
Community
  • 1
  • 1
Shiva Nandan
  • 1,835
  • 1
  • 13
  • 11
4

With

echo get_cfg_var('cfg_file_path');

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

abhilashv
  • 1,418
  • 1
  • 13
  • 18
4

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

M5M400
  • 41
  • 1
4

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 7.2.xxx 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)

Vladimir Vukanac
  • 944
  • 16
  • 29
4

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.

Sumit Kumar Gupta
  • 2,132
  • 1
  • 22
  • 21
2

if it is centos with php 5.3 installed.

sudo yum install php53-imap

and restart apache

sudo /sbin/service httpd restart

or

sudo service apache2 restart
double-beep
  • 5,031
  • 17
  • 33
  • 41
Robert
  • 41
  • 5
  • one can omit typing the /sbin - because that path is commonly contained within root's $PATH environment. Just check /root/.bashrc once - and you'll see what is given. – Martin Zeitler Feb 25 '14 at 12:47
1

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

1

For Xampp 7.3.9

PHP.ini line 913:

;extension=imap

You should remove the semicolon.

double-beep
  • 5,031
  • 17
  • 33
  • 41
0

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

;extension=php_imap.dll

to

extension=php_imap.dll
Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
0

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

https://packages.ubuntu.com/xenial/all/php-imap/download.

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

Amal
  • 62
  • 1
  • 8