将每个用户名添加到WordPress中的wp_mail()

时间:2017-05-01 19:55:50

标签: wordpress email

我有一个前端表单,然后向某个用户角色中的所有用户发送电子邮件。它发送正常但在电子邮件本身内,它在所有收件人电子邮件中打印出最后用户的名字(来自用户角色中的用户列表)。

我确定这与我设置错误的数组和/或变量/ s有关。任何帮助将不胜感激每个用户将如何收到电子邮件,并将打印出正确的名称!

这是我的代码:

// Get users and their roles.
$user_args = array(
'role__in' => 'test_role', 
'orderby'  => 'user_nicename',
 'order'    => 'ASC'
);

$users = get_users($user_args);

$user_name_list = array();

foreach ( $users as $user ) :
    $user_email_list[] = $user->user_email;
    $user_name_list[]  = $user->display_name;
endforeach;

$user_name = $user_name_list;

// This is part of the code that adds their username to the email content.
$body  = '<p style="margin-bottom: 20px;">Hello ' . $user_name . ',</p>';

1 个答案:

答案 0 :(得分:0)

我修改了代码,实际上是打印数组。

// Get users and their roles.
$user_args = array(
'role__in' => 'test_role', 
'orderby'  => 'user_nicename',
 'order'    => 'ASC'
);

$users = get_users($user_args);

$user_name_list = array();

foreach ( $users as $user ) :
    $user_email_list[] = $user->user_email;
    $user_name_list[]  = $user->display_name;

  $user_info = get_userdata($user-> ID);
  $username = $user_info->user_login;
  $first_name = $user_info->first_name;
  $last_name = $user_info->last_name;

endforeach;

$user_name = $user_name_list;

// This is part of the code that adds their username to the email content.
$body  = '<p style="margin-bottom: 20px;">Hello ' . $user_name . ',</p>';

可能会有所帮助。