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 = '[email protected]'; $password = 'mypassword'; /* try to connect */ $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' .imap_last_error());
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.
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
orsudo
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With