Adding a list of emails to wp_mail() in WordPress

2019-07-14 06:33发布

I am trying to add a list of email addresses to wp_mail() from a certain user role. I have a comma delimited list stored as $user_email_list but cannot get that to output into the $multiple_recipients array.

Any help would be greatly appreciated.

// Get users and their roles, create list of emails to send notification to.
    $user_args = array(
      'role__in' => 'test_role', 
      'orderby'  => 'user_nicename',
      'order'    => 'ASC'
    );

    $users = get_users($user_args);

    foreach ( $users as $user ) :
      $user_email_list = $user->user_email . ', ';
    endforeach;

// Email Data
    $multiple_recipients = array(
        $user_email_list
    );
    $subject = $post->post_title;
    $body    = $post->post_content;

1条回答
叛逆
2楼-- · 2019-07-14 07:18

Update your code of foreach and check your $multiple_recipients variable at final, it would be comma separated value.

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

$multiple_recipients = implode(', ', $user_email_list);
查看更多
登录 后发表回答