如何更改用于注册的电子邮件地址?

时间:2018-12-27 作者:rraallvv

我想有一个不同的电子邮件地址,该地址用于网站管理员发送注册电子邮件和电子邮件通知。例如,我使用admin@example.com 但我需要所有注册和通知邮件no-reply@example.com

我被告知可以从WP仪表板更改我的帐户电子邮件地址Settings > General, 但我启用了多站点选项,所以我想该选项现在位于Network Settings > Network Admin Email, 然而,我找不到一种方法来设置一个不同的电子邮件地址,只用于注册和设置中出现的选项中的通知。

另外,有人向我指出,使用SMTP,我可以用Gmail处理不同的电子邮件地址,但我的服务器在Cloudflare之后,我已经在API模式下安装了SendGrid插件,因为使用SMTP插件,我真正服务器的IP地址将使用SMTP在电子邮件头中发送。

1 个回复
SO网友:butlerblog

既然你提到了SMTP,我要提到的是,在你决定如何处理“发件人”地址之前,你可能需要做出这个决定,因为最终你选择的方向可能会影响你应该如何处理“发件人”地址。

“简单”的方法是只过滤“发件人”地址-如果您不使用SMTP发送。

下面是它的样子:

/**
 * Start by getting the subject line of the notification
 * email. This assumes the use of the use of the WP default,
 * so if some other process of registration is used, then
 * change accordingly.
 */
add_filter( \'wp_new_user_notification_email\', \'my_get_notification_subject\' );
function my_get_notification_subject( $args ) {
    global $my_notifiction_subject;
    $my_notifiction_subject = $args[\'subject\'];
    return $args;
}

/**
 * Filter for wp_mail so that we can check the subject line
 * of the email being sent against the value of the default
 * notification email (set in the filter above). If the 
 * message is the notification email, then set a global 
 * that we can check in the "from" and "from_name" filters
 * to change the "from" info.
 */
add_filter( \'wp_mail\', \'my_check_wp_mail\' );
function my_check_wp_mail( $args ) {
    // Needed globals.
    global $my_notifiction_subject, $my_change_from;
    // Start with setting change "from" notification to false.
    $my_change_from = false;
    if ( isset( $my_notifiction_subject ) ) {
        if ( $args[\'subject\'] == $my_notifiction_subject ) {
            $my_change_from = true;
        }
    }
    return $args;
}

/**
 * If the wp_mail filter set the $my_change_from global
 * to true, then the email being sent is the notification
 * email, so we\'ll use this filter to change the "from"
 * address.
 */
add_filter( \'wp_mail_from\', \'my_wp_mail_from\' );
function my_wp_mail_from( $from_email ) {
    global $my_change_from;
    if ( $my_change_from ) {
        $from_email = "no-reply@mydomain.com";
    }
    return $from_email;
}

/**
 * Same as above but it changes the "from name" to go
 * with the address.
 */
add_filter( \'wp_mail_from_name\', \'my_wp_mail_from_name\' );
function my_wp_mail_from_name( $from_name ) {
    global $my_change_from;
    if ( $my_change_from ) {
        $from_name = "No Reply";
    }
    return $from_name;
}
对上述内容的解释(尽管每个过滤器都有注释):

使用wp_new_user_notification_email 筛选以使用通知电子邮件主题行的值设置全局(以便我们可以在另一个函数中提取)。注意,这假定默认的WP新用户通知。如果正在使用其他流程,或者默认值以某种方式发生了更改,则需要进行更改以适应这种情况wp_mail 过滤器(“过滤器”-不要与wp_mail() 函数)对照我们在中设置的全局变量检查主题行wp_new_user_notification_email 滤器这将告诉您发送的电子邮件是否是新用户通知。如果是,请使用全局变量切换布尔值(true | false),以确定发件人电子邮件是否需要更改wp_mail_from 和wp_mail_from_name 用于检查的过滤器$my_change_from (在#2中提到的全局布尔值),以确定是否更改发件人地址和名称还有其他一些方法可以实现同样的概念-这只是一种可能的方法。

如果您最终使用SMTP,那么情况就不同了,因为这行不通。在这种情况下,您需要这两个的实际地址,并且在处理时需要基于该信息建立连接phpmailer_init.

相关推荐

Register email as username

我在Wordpress多站点中使用Wordpress 4.9.4,我需要允许用户使用其电子邮件地址作为用户名进行注册。查看代码,我发现阻止我完成的是ms函数。php有一个正则表达式过滤除a-z以外的其他字符的用户名,因此当电子邮件是用户名时,我会得到“用户名只能包含小写字母(a-z)和数字。”我在ms函数中更改了该正则表达式。php,它工作得很好。现在我的问题是,我不想每次更新wordpress时都重新编码wpmu\\u validate\\u user\\u signup函数。在这个问题上有什么好的做法