24

I'm using PHP 7.2 on OS X El Capitan, installed using Homebrew (of course). Now I'd like to use some IMAP functions from PHP's IMAP extension, but no matter what I search for, I can't find a way to add the extension on OSX.

Some things I've tried... I have, of course, tried the most commonly recommended approach:

$ brew reinstall php --with-imap

Yet this fails, returning:

Warning: php: this formula has no --with-imap option so it will be ignored!

Another method, which I found mentioned in passing, also fails:

$ brew install php72-imap

Error: No available formula with the name "php72-imap" 
==> Searching for a previously deleted formula (in the last month)...
Warning: homebrew/core is shallow clone. To get complete history run:
  git -C "$(brew --repo homebrew/core)" fetch --unshallow

Error: No previously deleted formula found.
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching taps...
==> Searching taps on GitHub...
Error: No formulae found in taps.

I'm not exactly sure which direction to go in with this. I'm sure there's an easy, probably documented, way of doing this, but I am yet to find it. Perhaps I'm just looking in the wrong places and using the wrong search terms...

Laef
  • 1,086
  • 2
  • 11
  • 27
  • Looks like IMAP extension was removed from Homebrew's PHP https://github.com/Homebrew/homebrew-core/pull/25579. – kfriend Jul 20 '18 at 02:31

6 Answers6

37

Kevin Abel is providing some of the PHP extensions removed from Homebrew/core. You can install the IMAP extension with:

brew tap kabel/php-ext
brew install php-imap

To install a specific version, such as 7.2 use:

brew install php@7.2-imap
Diego Vieira
  • 1,150
  • 2
  • 13
  • 31
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
  • 2
    This does not provide IMAP extension for 7.2. – Tom Nov 02 '18 at 09:46
  • There is an IMAP extension for PHP 7.1 : https://github.com/kabel/homebrew-php-ext/blob/master/Formula/php%407.1-imap.rb. You can probably adapt it to 7.2 without too much difficulties. – Ortomala Lokni Nov 02 '18 at 14:38
  • While this answer give you good general instructions on how to install PHP extensions, it will install the latest PHP version available and NOT the one you need. I went with the answer Alex provided because it's more specific. – phoenix Mar 27 '20 at 20:15
17

Here is how I solved this problem under Mojave:

First, I installed IMAP module for PHP 7.2

brew install kabel/php-ext/php@7.2-imap

Secondly I copied the imap.so from installed folder to the 'extension_dir' used by php.ini

sudo cp /usr/local/lib/php/20170718/imap.so to /usr/local/lib/php/pecl/20170718
Alex
  • 5,510
  • 8
  • 35
  • 54
  • Thank you thank you! This worked for me in PHP 7.4 (just changing the brew install to 7.4 accordingly) – Maya McKela Jun 27 '20 at 03:23
  • Thanks! This works for 8.0 as well: brew install kabel/php-ext/php@8.0-imap – phoenix May 14 '21 at 17:02
  • This is the way to go. I'm using packages from the the tap provided by shivammathur but the method remains pretty much the same, just that I had to add the tap for extensions first. Cheers! – Fr0zenFyr Oct 28 '21 at 07:50
15

There is a much better way to recompile php with IMAP extension directly using Homebrew.

UPDATE 2023/09: this procedure works even with the php8. Just use php instead of versioned formula php@7.4. Please note openssl has changed version to openssl@3.

  1. Run brew edit php@7.4

  2. Add depends_on "imap-uw" at the end of the depends_on section

  3. Check which version of openssl is in the depends_on section

  4. Add --with-imap=#{Formula["imap-uw"].opt_prefix} at the end of the --with section

  5. Add --with-imap-ssl=#{Formula["openssl@1.1"].opt_prefix} after --with-imap. Check sure it is the same version as in the depends_on section

  6. Run HOMEBREW_NO_INSTALL_FROM_API=1 brew reinstall --build-from-source php@7.4
    UPDATE 2023/02: If you dont prepend with HOMEBREW_NO_INSTALL_FROM_API=1, then modified file is not used. Behavior changed from 2023/01, more on https://github.com/Homebrew/brew/pull/14412

  7. It is not necessary to enable the php_imap.so extension in php.ini, because it is already compiled into PHP. You can check phpinfo();

In case of a formula update in the feature, just edit the formula again and reinstall with --build-from-source.

Vantomas
  • 151
  • 1
  • 4
12

After expired kabel/php-ext/php@7.2-imap, I used another tap:

brew tap shivammathur/php

brew tap shivammathur/extensions

brew install imap@7.2
Cainã Max Couto-Silva
  • 4,839
  • 1
  • 11
  • 35
Aikulian
  • 121
  • 1
  • 2
11

This answer for those who prefer installing imap ext using native commands without adding other taps or smth else.

In short we need to compile extension from sources. Ok, here is the process.

$ # Download sources from php.net of already installed php version. 
$ cd ~/Downloads
$ wget https://www.php.net/distributions/php-7.3.5.tar.gz
$ gunzip php-7.3.5.tar.gz
$ tar xvf php-7.3.5.tar
$ # Go to ext dir 
$ cd php-7.3.5/ext/imap
$ # prepare extension using phpize command, you should 
$ # ensure that you use phpize of proper version from 
$ # already installed php version as checking the API version for example
$ phpize
$ # prepare dependencies
$ # install openssl and imap
$ brew install openssl
$ brew install imap-uw
$ # after all installation check the installed paths of the exts
$ ./configure --with-kerberos --with-imap-ssl=/usr/local/Cellar/openssl/1.0.2r/ --with-imap=/usr/local/Cellar/imap-uw/2007f/
$ make
$ # get extension dir 
$ php -i | grep extension_dir
extension_dir => /usr/local/lib/php/pecl/20180731 => /usr/local/lib/php/pecl/20180731
$ cp modules/imap.so /usr/local/lib/php/pecl/20180731/
$ # add extension to your php.ini
# [imap]
# extension="imap.so"

That's it. Be lucky!

wtorsi
  • 692
  • 2
  • 8
  • 27
1

For those who have trouble in Mojave, I have forked the repository and fixed it use: brew tap vishal-sancheti/php-ext instead

  • unfortunately it does not work - Error: Cannot tap vishal-sancheti/php-ext: invalid syntax in tap! – seven Jan 08 '21 at 15:55