既然你提到了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.