致命错误:在 PHP 中调用未定义的函数 imap_open()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9654453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:22:46  来源:igfitidea点击:

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

phpimap

提问by Sumit

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

我正在尝试通过我的本地主机访问我的 gmail 帐户。但是,我得到了回应:

Fatal error: Call to undefined function imap_open()

致命错误:调用未定义的函数 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());

回答by Lekensteyn

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:

很简单,在您的 PHP 安装中未激活 IMAP 扩展。默认情况下启用。如果您的本地安装在 Windows 上运行 XAMPP,则必须按照XAMPP 常见问题解答中的说明启用它:

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.

PHP 的 IMAP 支持在哪里?

默认情况下,XAMPP 中禁用了对 PHP 的 IMAP 支持,因为在某些家庭版本(如 Windows 98)中存在一些神秘的初始化错误。使用 NT 系统的人可以"\xampp\php\php.ini"通过删除开头的分号来打开文件 以激活 php 扩展线";extension=php_imap.dll"。应该: extension=php_imap.dll

现在重新启动 Apache,IMAP 应该可以工作了。您可以对每个扩展使用相同的步骤,默认配置中未启用。

回答by Martin Zeitler

The Installation Procedureis 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 suor sudo.

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

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

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

a) 首先,需要用户权限提升,通过命令su或获得sudo

b) 然后可以使用包管理器安装缺少的 PHP 模块。

c) 之后,需要重新启动apache2HTTP 守护进程来加载模块。

d) 最后,可以运行php -m | grep imap以查看 PHP 模块现在是否可用。

On Ubuntuthe APTpackage php5-imap(or php-imap) can bei installed with apt-get:

Ubuntu 上,可以使用以下命令安装APT软件包php5-imap(或php-imapapt-get

apt-get install php5-imap
service apache2 restart

On Debian, the APTpackage php5-imapcan be installed aptitude(or apt-get):

Debian 上,可以安装(或)该APT软件包:php5-imapaptitudeapt-get

aptitude install php5-imap
apache2ctl graceful

On CentOSand Fedorathe RPMpackage php-imapcan be installed with yum(hint: the name of the package might be something alike php56w-imapor php71w-imap, when using Webtaticrepo):

CentOSFedora 上,可以使用以下命令安装该RPM软件包(提示:软件包名称可能与或类似,使用Webtatic 存储库时):php-imapyumphp56w-imapphp71w-imap

yum install php-imap
service httpd restart

On systemdsystems, while using systemd units, the command to restart unit httpd.serviceis:

systemd系统上,使用systemd 单元时,重启单元的命令httpd.service是:

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.inifile (at least on Ubuntu 12.04) and a PHP module must at most be referenced once. Using INIsnippets to load modules is suggested, while the the directory /etc/php5/conf.d/(that path may also vary) is being scanned for INIfiles:

发生这种情况是因为它在默认php.ini文件中被引用(至少在 Ubuntu 12.04 上)并且一个 PHP 模块最多必须被引用一次INI建议使用片段加载模块,同时/etc/php5/conf.d/正在扫描目录(该路径也可能不同)以查找INI文件:

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

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

Ubuntu 还具有管理 PHP 模块的专有命令,在重新启动 Web 服务器之前执行:

php5enmod imap
php5dismod imap

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

一旦IMAP模块被加载到服务器中,PHP IMAP 函数就会变得可用;最佳实践可能是,在尝试使用模块之前检查模块是否已加载

回答by pramod kadam

In Ubuntu for install imap use

在Ubuntu中安装imap使用

sudo apt-get install php-imap

Ubuntu 14.04 and above use

Ubuntu 14.04 及以上使用

sudo apt-get install php5-imap

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

并且默认情况下 PHP 未启用 imap,因此请使用此命令启用 imap 扩展

sudo php5enmod imap

Then restart your Apache

然后重启你的Apache

sudo service apache2 restart

回答by Shiva Nandan

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

如果您使用的是 linux,请编辑/etc/php/php.ini(或者您必须在 /etc/php5/cli/conf.d 中创建一个新的扩展导入文件)文件,以便添加 imap 共享对象文件,然后重新启动 apache 服务器。取消注释

;extension=imap.so

so that it becomes like this:

使它变成这样:

extension=imap.so

Then, restart the apache by

然后,通过以下方式重新启动apache

# /etc/rc.d/httpd restart

回答by abhilashv

With

echo get_cfg_var('cfg_file_path');

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

您可以找出这个 php 实例使用了哪个 php.ini。

回答by M5M400

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.

在从 Ubuntu 12.04 迁移到 14.04 的过程中,我也偶然发现了这一点,并想分享从 Ubuntu 14.04 LTS 开始,默认情况下似乎不再加载 IMAP 扩展。

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

应该为 apache 修复它。对于 CLI,在 /etc/php5/cli/conf.d 中做同样的事情

回答by Vladimir Vukanac

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

在带有 Homebrew 的 Mac OS X 上,很明显,由于提供的错误我们无法运行,PHP 已经安装:

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

更新:最新版本brew instal php --with-imap将不再适用!!!

$ 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 reinstallit:

所以,我们必须重新安装它:

$ brew reinstall php72 --with-imap

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

这将需要一段时间:-)(内置于 8 分 17 秒)

回答by Robert

if it is centos with php 5.3 installed.

如果是安装了 php 5.3 的 centos。

sudo yum install php53-imap

and restart apache

并重新启动apache

sudo /sbin/service httpd restart

or

或者

sudo service apache2 restart

回答by Carlos Martinez Ival

Ubuntu with Nginx and PHP-FPM 7 use this:

带有 Nginx 和 PHP-FPM 7 的 Ubuntu 使用这个:

sudo apt-get install php-imap

须藤 apt-get 安装 php-imap

service php7.0-fpm restart service ngnix restart

服务 php7.0-fpm 重启 服务 ngnix 重启

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

检查模块是否已安装 php -m | grep 映射

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

模块 imap 的配置将在 cli php.ini 和 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

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

回答by Bashirpour

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:

如果您的本地安装在 Windows 上运行 XAMPP,这就足够了:您可以打开文件“\xampp\php\php.ini”,通过删除“;extension=php_imap.dll”行的开头分号来激活 php 扩展。它应该是:

;extension=php_imap.dll

to

extension=php_imap.dll