WordPress拒绝发送邮件,“ ...您的主机可能已禁用mail()函数”
-
-
请提供[调试信息](http://codex.wordpress.org/Debugging_in_WordPress)Please provide [debugging information](http://codex.wordpress.org/Debugging_in_WordPress)
- 0
- 2013-04-08
- s_ha_dum
-
9 个回答
- 投票数
-
- 2013-04-08
分步操作:首先找到出现错误消息的文件.我使用Notepad ++和 CTRL + F 命令搜索文件.最好仅搜索错误消息的前几个单词,因为某些错误消息是由不同的消息组合而成的.
您的错误消息仅出现在
wp-login.php
中.因此,让我们看一下为什么会发生此错误.if ( $message && !wp_mail($user_email, $title, $message) )
有两个条件.
$message
必须为true(非空字符串,false或null).而且wp_mail()
不应返回false.上面一行,有一个过滤器
$message = apply_filters('retrieve_password_message', $message, $key);
,因此插件(或主题)有可能使用此过滤器并且返回一个不为真的值(空字符串,false,null等).但是检查
wp_mail()
是否正常工作要容易得多.编写一个小插件,向自己发送测试邮件:<?php /** * Plugin Name: Stackexchange Testplugin * Plugin URI: http://yoda.neun12.de * Description: Send me a test email * Version: 0.1 * Author: Ralf Albert * Author URI: http://yoda.neun12.de * Text Domain: * Domain Path: * Network: * License: GPLv3 */ namespace WordPressStackexchange; add_action( 'init', __NAMESPACE__ . '\plugin_init' ); function plugin_init(){ $to = '[email protected]'; $subject = 'Testemail'; $message = 'FooBarBaz Testmail is working'; wp_mail( $to, $subject, $message ); }
(这是PHP5.3代码.如果您正在运行PHP5.2,请删除名称空间内容)
插件应在激活后立即发送测试邮件.如果没有,应该调用一些后端页面(例如仪表板).
如果测试邮件没有到达,则可能是
wp_mail()
出现问题.因此,打开调试:define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', true ); @ini_set( 'display_errors',1 );
将此代码放入您的
wp-config.php
中,然后尝试向自己发送测试邮件.现在,您应该获得一些错误消息,并且还应该将它们记录到wp-content/debug.log
中(如果由插件和/或主题引起的更多错误,调试日志会变得非常大).这时,如果
wp_mail()
失败,您将获得很好的信息,如果失败,为什么.如果wp_mail()
正常工作并且测试邮件到达,请返回顶部,找出为什么$message
不正确的原因.如果您对
wp_mail()
有疑问,请记住wp_mail()
不使用PHP的mail()
函数. WordPress使用PHP类( PHPMailer ).也许您只需要使用SMTP的插件而不是sendmail.或者问题出在另一个地方.我们不知道你必须调查.Step by step: First find the file where the error message appear. I use Notepad++ and the CTRL + F command to search in files. It is a good idea to search only the first few words of the error message, because some error messages are combined of different messages.
Your error message appear in
wp-login.php
and holy luck, only there. So let's have a look why this error could occur.if ( $message && !wp_mail($user_email, $title, $message) )
There are two conditions.
$message
have to be true (not an empty string, not false, not null, etc). Andwp_mail()
shouldn't return false.One line above, there is a filter
$message = apply_filters('retrieve_password_message', $message, $key);
, so it is possible that a plugin (or theme) use this filter and returns a value that is not true (empty string, false, null, etc.).But it is much easier to check if
wp_mail()
is working or not. Write a small plugin to send a test mail to yourself:<?php /** * Plugin Name: Stackexchange Testplugin * Plugin URI: http://yoda.neun12.de * Description: Send me a test email * Version: 0.1 * Author: Ralf Albert * Author URI: http://yoda.neun12.de * Text Domain: * Domain Path: * Network: * License: GPLv3 */ namespace WordPressStackexchange; add_action( 'init', __NAMESPACE__ . '\plugin_init' ); function plugin_init(){ $to = '[email protected]'; $subject = 'Testemail'; $message = 'FooBarBaz Testmail is working'; wp_mail( $to, $subject, $message ); }
(This is PHP5.3 code. If you are running PHP5.2, remove the namespace things)
The plugin should send a testmail immediately after activation. If not, calling some backend pages (e.g. dashboard) should do it.
If the testmail does not arrive, then you probably have an issue with
wp_mail()
. So turn on debugging:define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', true ); @ini_set( 'display_errors',1 );
Put this code into your
wp-config.php
and retry sending yourself a testmail. Now you should get some error messages and they also should be logged intowp-content/debug.log
(The debug log can grow very large if there are more errors caused by plugins and/or themes).At this point, you got good informations if
wp_mail()
fails and if so, why. Ifwp_mail()
work correctly and the testmail arrived, go back to top and find out why$message
is not true.If you have issues with
wp_mail()
, so keep in mind thatwp_mail()
does not use PHPsmail()
function. WordPress use a PHP class (PHPMailer). Maybe you just need a plugin to use SMTP instead of sendmail. Or the problem is located at another place. We don't know. You have to investigate.-
是的,我尝试深入研究内核,这也将我引向了PHPMailer,它实际上*确实*使用php的`mail()`.至少在某些情况下(请参阅`wp-includes/class-phpmailer.php`中的732行.我没有访问ftp atm的权限,但是我会尽快尝试您的建议.. 非常感谢!Yeah i tried digging into the core and it also lead me to PHPMailer, and it actually *does* use php's `mail()`. At least in some cases (see line 732 in `wp-includes/class-phpmailer.php`. I don't have access to the ftp atm but i will try your suggestions as soon as i can. Surely this must lead me somewhere. Thanks a lot!
- 0
- 2013-04-08
- qwerty
-
我测试了`wp_mail()`,它似乎工作正常,我按预期收到了邮件.WP仍然不会发送评论/密码重置电子邮件,并且日志文件中没有任何内容(未创建),因此我尝试安装SMTP邮件插件并设置了新的电子邮件帐户WordPress的.现在可以使用了,但是我仍然不明白为什么以前无法发送.谢谢!I tested `wp_mail()` and it seems to work fine, i received the mail as expected. WP still wouldn't send the comment/password-reset emails though, and i didn't get anything in the log file (it wasn't created), so i tried installing an SMTP mail plugin and set up a new email account for Wordpress. It works now but i still don't understand why it couldn't send before. Thanks!
- 0
- 2013-04-09
- qwerty
-
我没有任何错误,甚至没有邮件I'm not getting any error and even not mail
- 0
- 2017-08-19
- baldraider
-
- 2014-12-11
这是一个超级烦人的错误消息,因为它可能是很多东西,而且并没有显示出实际错误(在代码的其他部分中通常是静默的).
当
wp_mail()
函数返回false时出现此错误,如果phpmailer-&gt; Send()
返回false或引发异常,则可能发生这种错误./p>
如何从PHP的
mail()
函数显示警告这些通常默认情况下是静音的,但是不幸的是WordPress从未捕获它们.要显示它们,只需将
wp-includes/class-phpmailer.php
中@mail(...
中的@
符号删除,code>mailPassthru()函数:if(ini_get('safe_mode')||!($this-&gt; UseSendmailOptions)){ $ rt=@mail($to,$this-&gt;encodeHeader($this-&gt; secureHeader($ subject)),$body,$ header); }其他{ $ rt=@mail($to,$this-&gt;encodeHeader($this-&gt; secureHeader($ subject)),$body,$ header,$params); }
如何查找其他可能的原因:
-
在
/wp-includes/pluggable.php
中的wp_mail()
底部添加一行://发送! 尝试{ 返回$phpmailer-&gt; Send(); } catch(phpmailerException $e){ //-------------下一行是要添加的那一行------------------- 如果(WP_DEBUG)回显'&lt;pre&gt;' .esc_html(print_r($e,TRUE)). '&lt;/pre&gt;'; 返回false; }
-
它将转储引发异常的位置的完整详细信息.不幸的是,它有时会包含以下无用的异常消息:" 无法实例化邮件功能".是的,感谢WordPress,这真的很有帮助.
-
通过查看异常,您可以找到错误的行号,并希望可以通过代码追溯到此,以找到真正的原因.
祝你好运.希望WordPress在将来的某个时候可以改善电子邮件错误处理.
This is a super annoying error message as it could be many things, and it doesn't reveal the actual error (which is often silenced in other parts of the code).
This error appears when the
wp_mail()
function returns false, which in turn could happen ifphpmailer->Send()
returns false or raises an exception.How to display warnings from PHP's
mail()
functionThese are normally silenced by default, but unfortunately WordPress never captures them. To show them, simply remove the
@
signs from@mail(...
inwp-includes/class-phpmailer.php
in themailPassthru()
function:if (ini_get('safe_mode') || !($this->UseSendmailOptions)) { $rt = @mail($to, $this->encodeHeader($this->secureHeader($subject)), $body, $header); } else { $rt = @mail($to, $this->encodeHeader($this->secureHeader($subject)), $body, $header, $params); }
How to hunt down other possible causes:
Add a single line to the bottom of
wp_mail()
in/wp-includes/pluggable.php
:// Send! try { return $phpmailer->Send(); } catch ( phpmailerException $e ) { //------------- This next line is the one to add ------------------- if (WP_DEBUG) echo '<pre>' . esc_html(print_r($e, TRUE)) . '</pre>'; return false; }
It will dump the full details of where the exception was raised. Unfortunately it sometimes includes this unhelpful exception message: "Could not instantiate mail function". Yeah thanks WordPress, that's real helpful.
By looking at the exception you can find the line number of the error, and can hopefully trace it back through the code to find the real cause.
Good luck. Hopefully WordPress improves email error handling at some point in the future.
-
- 2017-05-03
我在Amazon EC2上使用Ubuntu服务器时遇到了相同的问题.在使用重置密码链接时出现问题,其他通知电子邮件也无法正常工作.
这是对我有用的解决方案.Word-press使用
wp_mail()
函数发送需要使用PHPMailer
类的电子邮件,该类使用存储在/usr/sbin/sendmail
.首先使用此简单的php函数检查php邮件
<?php $to = "[email protected]"; $subject = "Test Email Function"; $txt = "Hello world!"; $headers = "From: [email protected]" . "\r\n" . "CC: [email protected]"; mail($to,$subject,$txt,$headers); ?>
如果这不起作用,则需要安装phpmailer. 使用此命令在Ubuntu服务器上安装php邮件.
sudo apt-get install sendmail
然后检查单词按电子邮件功能.
I has same issue with Ubuntu server on Amazon EC2.I get issue while using reset password link and also other notification email were not working.
So here is solutions which worked for me.Word-press used
wp_mail()
function to send email which needPHPMailer
class which used php mailer stored in/usr/sbin/sendmail
.Use this simple php function first to check php mail
<?php $to = "[email protected]"; $subject = "Test Email Function"; $txt = "Hello world!"; $headers = "From: [email protected]" . "\r\n" . "CC: [email protected]"; mail($to,$subject,$txt,$headers); ?>
If this is not working then you need to install php mailer. Use this command to install php mail on Ubuntu server.
sudo apt-get install sendmail
Then check word-press email functions.
-
这个答案是任何人都应该尝试的答案,这是要走的路this answer is the one anyone should try before any other answers, this is the way to go
- 0
- 2019-01-25
- hatenine
-
- 2017-02-04
如果这里其他好的答案没有帮助,请尝试以下操作:
我遇到了同样的问题,在WordPress的任何建议中我都找不到解决方案.
然后,我开始调查是否是PHP安装本身禁用了邮件功能,但这些都不起作用.一切看起来都已正确配置.
当我将服务器升级到使用SELinux(安全性增强Linux)的CentOS 7时,所有这些问题对我来说都是开始的.最近两周我在SELinux上了解到的是,如果某些方法不起作用,但是一切看起来都应该正常工作……这意味着SELinux在无声无息地在后台阻止了您.
还有中提琴.
如果您正在运行并且使用SELinux的操作系统,只需以root用户身份执行以下命令:
setsebool -P httpd_can_sendmail=1
有一个安全设置,可以从本质上阻止Web服务器发送电子邮件.当您按下该开关并告诉SELinux时,网络服务器可以发送电子邮件了,一切突然都正常了.
If the other great answers here don't help, try this:
I encountered this same problem and nothing I could find in any of the suggestions for WordPress solved it for me.
Then I started investigating if it was the PHP installation itself that had disabled the mail function, but none of that worked either. Everything looked like it was configured properly.
All of these problems started for me once I upgraded my server to CentOS 7 which uses SELinux (Security Enhanced Linux) and what I've learned in the last couple of weeks with SELinux is that if something isn't working, but everything looks like it should be working... that means SELinux is silently and secretly blocking you in the background.
And viola.
If you are running and OS that uses SELinux, just execute the following command as root:
setsebool -P httpd_can_sendmail=1
There is a security setting that inherently prevents the webserver from sending email. When you flip that switch and tell SELinux it's ok for the webserver to send email, everything suddenly works.
-
- 2014-01-16
我今天碰到了在我的情况下,发生这种情况是因为服务器的hosts文件具有与电子邮件地址相同的域名,指向localhost.mx记录指向另一台服务器,但是hosts文件覆盖了DNS,而WP尝试在本地传递电子邮件.从主机文件中删除域并重新启动sendmail解决了此问题.I ran into this today; in my case the situation happened because the server's hosts file has the same domain name of the email address, pointing to localhost. The mx record points to a different server, but the hosts file is overriding DNS and WP is trying to deliver the email locally. Removing the domain from the hosts file and restarting sendmail resolved this issue.
-
- 2014-05-30
我不知道这是否仍然与您相关,但是由于没有选择答案,我想让我尝试一下.
实际上,我遇到了完全相同的问题,因为我的openshift主持人今天突然全部放弃,并停止发送邮件.深入研究代码和法典,我了解了wp_mail()函数,最后google将我引到这里,我看到了如何覆盖它.
基于@ Ralf912的答案,我对脚本进行了一些修改,以便代码使用sendgrid.com的网络api发送邮件,而不是使用wordpress默认邮件(我认为:
<?php function sendgridmail($to, $subject, $message, $headers) { $url = 'https://api.sendgrid.com/'; //$user = 'yourUsername'; //$pass = 'yourPassword'; $params = array( 'api_user' => $user, 'api_key' => $pass, 'to' => $to, 'subject' => $subject, 'html' => '', 'text' => $message, 'from' => '[email protected]', ); $request = $url.'api/mail.send.json'; // Generate curl request $session = curl_init($request); // Tell curl to use HTTP POST curl_setopt ($session, CURLOPT_POST, true); // Tell curl that this is the body of the POST curl_setopt ($session, CURLOPT_POSTFIELDS, $params); // Tell curl not to return headers, but do return the response curl_setopt($session, CURLOPT_HEADER, false); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // obtain response $response = curl_exec($session); curl_close($session); // print everything out //print_r($response); } //only for testing: /*$to = '[email protected]'; $subject = 'Testemail'; $message = 'It works!!'; echo 'To is: ' + $to; #wp_mail( $to, $subject, $message, array() ); sendgridmail($to, $subject, $message, $headers); print_r('Just sent!');*/ if (!function_exists('wp_mail')) { function wp_mail($to, $subject, $message, $headers = '', $attachments = array()) { // use the PHP GnuPG library here to send mail. sendgridmail($to, $subject, $message, $headers); } } function plugin_init() { /* $to = '[email protected]'; $subject = 'Testemail'; $message = 'It works Live!'; //echo 'To is: ' + $to; wp_mail( $to, $subject, $message, array() ); //print_r('Just sent!');*/ }
成功了!
I don't know whether this is still relevant to you or not, but since there is no answer chosen, I thought let me give it a try once.
Actually, I had faced the exact same problem since my openshift host all of a suddenly gave way today and stopped sending mails. Digging through the code and codex, I came to know about the wp_mail() function and finally google led me here and I saw how it could be overridden.
Building on @Ralf912's answer, I modified the script a bit so that the code uses sendgrid.com's web api to send mails instead of wordpress default one (that I presume :
<?php function sendgridmail($to, $subject, $message, $headers) { $url = 'https://api.sendgrid.com/'; //$user = 'yourUsername'; //$pass = 'yourPassword'; $params = array( 'api_user' => $user, 'api_key' => $pass, 'to' => $to, 'subject' => $subject, 'html' => '', 'text' => $message, 'from' => '[email protected]', ); $request = $url.'api/mail.send.json'; // Generate curl request $session = curl_init($request); // Tell curl to use HTTP POST curl_setopt ($session, CURLOPT_POST, true); // Tell curl that this is the body of the POST curl_setopt ($session, CURLOPT_POSTFIELDS, $params); // Tell curl not to return headers, but do return the response curl_setopt($session, CURLOPT_HEADER, false); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // obtain response $response = curl_exec($session); curl_close($session); // print everything out //print_r($response); } //only for testing: /*$to = '[email protected]'; $subject = 'Testemail'; $message = 'It works!!'; echo 'To is: ' + $to; #wp_mail( $to, $subject, $message, array() ); sendgridmail($to, $subject, $message, $headers); print_r('Just sent!');*/ if (!function_exists('wp_mail')) { function wp_mail($to, $subject, $message, $headers = '', $attachments = array()) { // use the PHP GnuPG library here to send mail. sendgridmail($to, $subject, $message, $headers); } } function plugin_init() { /* $to = '[email protected]'; $subject = 'Testemail'; $message = 'It works Live!'; //echo 'To is: ' + $to; wp_mail( $to, $subject, $message, array() ); //print_r('Just sent!');*/ }
And it worked!
-
- 2016-08-23
我有同样的错误,两个函数(mail和wp_mail)都起作用,但是我仍然遇到这个烦人的错误.修复非常容易,但是花了我几个小时才找到原因.因此,我将在这里分享与您(或可能不一样)的问题的解决方案.
我尝试过mail()函数,但是它起作用了,但是当您对其进行测试时,没有在mail()函数中指定最后一个称为"parameters"的参数. WP确实使用它.
@mail("[email protected]",$title,$body,$headers,"[email protected]");
因此,基本上,带有标志" -f"的此参数(" [email protected]")使mail()函数检查"受信任的电子邮件"列表中是否列出了电子邮件地址"[email protected]" .
因此,如果没有,它将返回false,这会使wp_mail()返回false并导致错误消息.
因此,解决方案是要求托管服务商为您执行此操作,或者,如果您使用的是cPanel,只需为该地址添加电子邮件帐户,它将自动将其添加到"受信任的列表"中.
I had the same error, both functions(mail and wp_mail) worked, but I still had this annoying error. The fix was very easy, but it took me few hours to find the reason. So I will share here my solution on the problem which might be (or might not) the same with yours.
I tried mail() function and it worked, but when you test it you don't specify the last parameter called 'parameters' in mail() function. And WP does use it.
@mail("[email protected]",$title,$body,$headers,"[email protected]");
So, basically, this parameter ("[email protected]") with flag "-f" makes mail() function check if the email address "[email protected]" listed in the "trusted emails" list.
So if it doesn't, it returns false, which makes wp_mail() returns false and leads to the error message.
So, solution is to ask hoster to do this for you, or if you are using cPanel, just add email account for this address and it will automatically will add it into the "trusted list".
-
- 2019-05-31
它称为-管理用于通过脚本发送邮件的已注册电子邮件ID,例如(WordPress)
- 登录您的Cpanel.
- 转到电子邮件部分>,然后单击注册的电子邮件ID.
- 然后添加([email protected])或您的wordpress托管位置.即([email protected]).然后提交,则需要几分钟的时间来激活等待15分钟到1小时的等待时间,具体取决于您的托管服务提供商,然后它将起作用.
it called -Manage Registered Email-Ids For Sending Mails via Scripts ie.(Wordpress)
- Login your Cpanel.
- Go to Email Section > then Click Registered Email IDs.
- then add ([email protected]) or where your wordpress hosted. ie ([email protected]) . then submit, it takes few minute to activate wait 15minute to 1 hour depending to your hosting provider, then it will work.
-
- 2019-12-14
我这个错误已有很长时间了,并尝试了许多行不通的解决方案.我在AWS EC2上安装了自定义Wordpress.首先,请确保通过支持启用了您的AWS SES邮件,它们必须位于SES和EC2中的同一(或关闭)区域中. 我使用Google suite(gsuite)接收和发送电子邮件的电子邮件.
确保测试电子邮件通过AWS SES和Gsuite发送.
安装Wordpress插件WP Mail SMTP,使用选项"其他SMTP",从AWS SES获取您的SMTP凭据,这就是我遇到的问题.
您必须启用" SSL"复选框以进行加密,这对我来说将端口更改为465.最后,我的电子邮件测试成功地从Worpdress发送出去了.
I had this error for ages and tried so many solutions that didn't work. I have a custom Wordpress install on AWS EC2. First off ensure your AWS SES mail is enabled through support, they must be in the same (or close) region in SES and EC2. I used Google suite(gsuite) for email for receiving/sending mail.
Make sure the test email sends in AWS SES and Gsuite.
Install the Wordpress plugin WP Mail SMTP, use the option "Other SMTP", grab your SMTP credentials from AWS SES, this is where I got stuck.
You must enable the tick box "SSL" for Encryption, this changes the port to 465 for me. At last my email test sent from Worpdress successfully.
我最近在我的网站上实现了评论区域,并试图使电子邮件通知正常工作.做出新评论时,它似乎不想发送电子邮件通知.
仅查看PHP是否可以发送电子邮件,我尝试重置密码(因为您将通过邮件获得新密码),然后收到消息:
我已经选中了"设置"->"讨论"中的复选框,并且该电子邮件有效,因此这不是设置问题.我尝试创建一个PHP文件并使用
mail()
发送,并且发送成功.因此WordPress一定有些奇怪.有什么想法吗?