WordPress“phpmaeller_init”不适合我

时间:2021-02-03 作者:km onur

I add the following code to the functions.php file.

add_action( \'phpmailer_init\', \'my_phpmailer_example\' );
function my_phpmailer_example( $phpmailer ) {
    $phpmailer->isSMTP();     
    $phpmailer->Host = SMTP_HOST;
    $phpmailer->SMTPAuth = SMTP_AUTH;
    $phpmailer->Port = SMTP_PORT;
    $phpmailer->Username = SMTP_USER;
    $phpmailer->Password = SMTP_PASS;
    $phpmailer->SMTPSecure = SMTP_SECURE;
    $phpmailer->From = SMTP_FROM;
    $phpmailer->FromName = SMTP_NAME;
}

and wp-config.php

// SMTP email settings
define( \'SMTP_USER\', \'{email}\' );
define( \'SMTP_PASS\', \'{password}\' );
define( \'SMTP_HOST\', \'{server}\' );
define( \'SMTP_FROM\', \'{from}\' );
define( \'SMTP_NAME\', \'{name}\' );
define( \'SMTP_PORT\', \'465\' );
define( \'SMTP_SECURE\', \'ssl\' );
define( \'SMTP_AUTH\', true );

"wp-login.php?action=lostpassword" when I go here, I enter my e-mail address and click the send button,

enter image description here

"Failed to send e-mail. Your site may not be properly configured to send e-mail." I am getting this error.


But when I paste the same code into another WordPress with WooCommerce plugin installed, I forgot my WooCommerce password. Mail can be successfully sent from the front-end design.

Where is the problem? Am I doing something wrong?


3rd step

add_action( \'wp_mail_failed\', function ( $error ) {
    error_log( $error->get_error_message() );
} );

Not working (IDK). But I searched and found the code below,

add_action(\'wp_mail_failed\', \'log_mailer_errors\', 10, 1);
function log_mailer_errors( $wp_error ){
  $fn = ABSPATH . \'/mail.log\'; // say you\'ve got a mail.log file in your server root
  $fp = fopen($fn, \'a\');
  fputs($fp, "Mailer Error: " . $wp_error->get_error_message() ."\\n");
  fclose($fp);
}

This gave me the following error

Mailer Error: Invalid address:  (From): wordpress@localhost

I found the relevant location from pluggable.php.

Since I was working on localhost, it was emitting an invalid e-mail address because it did not create a valid TLD.

        if ( ! isset( $from_email ) ) {
            // Get the site domain and get rid of www.
            $sitename = wp_parse_url( network_home_url(), PHP_URL_HOST );
            if ( \'www.\' === substr( $sitename, 0, 4 ) ) {
                $sitename = substr( $sitename, 4 );
            }

            $from_email = \'wordpress@\' . $sitename;
        }

This means, "$phpmailer->From = SMTP_FROM;" instead of "add_filter (\'wp_mail_from\', \'set_from\');" should use.

function set_from()
{
    return \'[email protected]\';
}

add_filter(\'wp_mail_from\', \'set_from\');

Works now.

I don\'t understand why phpmailer_init doesn\'t overwrite from information.


By the way, the sender still appears to be what I defined in ($phpmailer-> From).

I guess this is some kind of bug and should be reported to WordPress?

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

问题出在哪里?我做错什么了吗?

您的代码对我来说似乎很好,因此尽管我无法给出问题的确切答案,但我认为这些可能有助于您解决问题:

首先,检查您的配置-例如,您是否使用了正确的(SMTP)用户名/电子邮件、密码、主机?

并确保SMTP_FROM 值是有效的email addresss — e、 g。[email protected] 而不是user@exampleUser <[email protected]>; 否则,将不会发送电子邮件。

如果您确定配置良好,那么由于您使用SMTP,请尝试启用类似的调试-将此添加到my_phpmailer_example() 功能:

// Note: I assumed you\'re using at least WordPress version 5.5.0

$phpmailer->SMTPDebug = PHPMailer\\PHPMailer\\SMTP::DEBUG_SERVER;
然后访问WordPress的密码重置页面并尝试重置密码,在下一页,如果PHPMailer无法发送电子邮件,则会在页面顶部显示错误消息。

如果无法执行上述SMTP调试(实际上这只是一个快速故障排除),请启用debugging in WordPress 并使用wp_mail_failed hook 要尝试捕获发送电子邮件时的任何错误,请将其添加到函数文件中:

add_action( \'wp_mail_failed\', function ( $error ) {
    error_log( $error->get_error_message() );
} );
然后检查wp-content/debug.log 归档并查看是否有任何相关错误/信息。

更新(抱歉,我忍不住要说这句话)我喜欢这个问题,很高兴你解决了这个问题

I guess this is some kind of bug and should be reported to WordPress?

也许吧,但如果他们能做些什么来避免问题的发生,这绝对是一件好事,特别是因为有很多人在localhost.

但万一你不知道,你可以使用虚拟主机,比如localhost.dev 这样就可以避免这个问题。

由于我使用的是localhost,它发出的电子邮件地址无效,因为它没有创建有效的TLD。

是的,我猜你已经在wp_mail() 代码(inpluggable.php):

/*
 * If we don\'t have an email from the input headers, default to wordpress@$sitename
 * Some hosts will block outgoing mail from this address if it doesn\'t exist,
 * but there\'s no easy alternative. Defaulting to admin_email might appear to be
 * another option, but some hosts may refuse to relay mail from an unknown domain.
 * See https://core.trac.wordpress.org/ticket/5007.
 */
所以是的,要么使用wp_mail_from hook 或者正如我上面所说的,使用虚拟主机。

我不明白为什么phpmailer_init 不覆盖from信息

因为WordPress/wp_mail() calls $phpmailer->setFrom() 设置From 属性,如果PHPMailer引发异常/错误(例如,由于电子邮件地址无效),则wp_mail() 退货false 永远不要点燃phpmailer_init 挂钩。

但是wp_mail_failed hook确实会被解雇,因此它是一种很好的方法,可以捕获使用wp_mail(). <(对不起,我没提过earlier 因为我认为SMTP调试也适用于您:))

By the way, the sender still appears to be what I defined in ($phpmailer->From).

是的,这很正常,因为setFrom()phpmailer_init 吊钩已启动。

事实上,当我说;在WordPress中启用调试;,我的意思是WP_DEBUGWP_DEBUG_LOG 常量到true; 如果error_log() 不写信给wp-content/debug.log (或通过WP_DEBUG_LOG 常数),则可以使用第2个和第3个参数error_log() 像这样:

add_action( \'wp_mail_failed\', function ( $error ) {
    // the "3" means write the message to the file as defined in the third parameter
    error_log( $error->get_error_message(), 3, WP_CONTENT_DIR . \'/debug.log\' );
} );
但无论如何,我希望这个答案/更新和这个问题也能帮助其他人。=)快乐的编码!

相关推荐

在静态页面中显示WordPress帖子的PHP代码不起作用

我有一个静态的网站主页。在同一个域上的Wordpress上有一个单独的博客(位于/博客)。六年前,我向一位朋友寻求帮助,在主页上显示该博客最近的两篇帖子,他通过以下方式做到了这一点:从索引更改主页。html收件人。php索引中包含以下代码。php function blog() { $url = \'MysiteURL.com/blog/feed/\';