When trying to access your email host using PHP
imap_open() function, you may see an error saying the function is undefined as follows:
Fatal error: Uncaught Error: Call to undefined function imap_open() in /site/index.php:8
This fatal error happens because
imap_open() is not a function included in PHP by default.
To use the
imap_open() function, you need to have the IMAP extension activated in your PHP installation.
Depending on what OS you use and how you get PHP on your computer, there are several ways you can add the PHP IMAP extension.
Add IMAP for Ubuntu
For Ubuntu OS, you need to install
php-imap package using
Please note that you may need to add
sudo to run the following commands:
# install php-imap apt install php-imap # enable php-imap phpenmod imap # restart apache server systemctl restart apache2 # or service apache2 restart
You should now be able to call the
Add IMAP for Windows
If you’re using PHP on Windows, the
php_imap.dll file should already be included under the
ext/ folder as follows:
This applies to PHP downloaded from windows.php.net and from XAMPP
You only need to activate the extension by editing the
php.ini file. Find where your
php.ini file is located by calling the
<?php phpinfo(); ?>
Open the file from your browser, and look for the
php.ini file path as in the example below:
php.ini file and uncomment the line where the IMAP extension is defined.
Make sure that the semicolon symbol before the
extension keyword is removed as follows:
Finally, restart your Apache server.
You should be able to run the
imap_open() function because the IMAP extension has been enabled.
For macOS with Homebrew
For PHP in macOS, you need to install the PHP extension using Homebrew third-party repositories (or tap).
PHP developer Shivam Matur has created a tap that you can add to your Homebrew installation.
Here are the steps to use PHP extensions tap. Note that you need to add the extension version as shown below:
# add PHP extensions tap brew tap shivammathur/extensions # install the imap extension v8.1 brew install shivammathur/extensions/[email protected] # or install the imap extension v7.4 brew install shivammathur/extensions/[email protected]
Once you installed the extension, Homebrew will automatically add the configuration file to your PHP installation.
Run PHP server using the following command:
php -S localhost:8000
Then open a file that runs the
imap_open() function. It should run without any error now.
If you’re using XAMPP on Mac, you can use the Windows guide as shown above to enable the IMAP extension.
The PHP error: Call to undefined function
imap_open() occurs when you don’t have the IMAP extension activated in your PHP installation.
To solve the error, you need to enable the IMAP extension. How to get the IMAP extension depends on the OS you use on your computer.
Once IMAP extension is activated, you can call the
I hope this tutorial has been useful for you. 😉