如何向所有Wordpress用户发送电子邮件

问题描述 投票:0回答:2

我正在尝试向所有用户发送自定义事件的电子邮件。为此,我编写了以下代码,但它无法正常工作。我究竟做错了什么?

//send mail to all users

$result= $wpdb->get_var( $wpdb->prepare("SELECT user_email FROM $table_users"));

foreach($result as $email) {

      $headers = "MIME-Version: 1.0" . "\r\n";
      $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
      $headers.= "From:mysite<[email protected]>\r\n";
      $message = "<b>Dear! user</b>,<br/><br/> A new share is available to purchase 
                        for amount $".$sellamt."<br>this is an automated mail.pls  don't reply to this mail. ";
      $send=mail($email,"mysite:New share Available to Purchase!",$message,$headers);
}

header('Location: /offer_csuccess');  
exit();
php wordpress email
2个回答
2
投票

使用get_users函数根据角色或您传递的任何参数获取所有用户。

$users = get_users();

foreach( $users as $user ) {

    wp_mail( $user->user_email, "New Share Available to Purchase!", $message );
}

同样使用WordPress,您每次都可以不提及标题。你只需要使用wp_mail。它确实发送电子邮件更简单。

希望它有帮助:)


0
投票

获取所有用户功能get_users();

$all_users = get_users();
foreach ($all_users as $user) {


      $headers = "MIME-Version: 1.0" . "\r\n";
      $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
      $headers.= "From:mysite<[email protected]>\r\n";
      $message = "<b>Dear! user</b>,<br/><br/> A new share is available to purchase 
                        for amount $".$sellamt."<br>this is an automated mail.pls  don't reply to this mail. ";
      $send=mail($user->user_email,"mysite:New share Available to Purchase!",$message,$headers);
}

 exit();
© www.soinside.com 2019 - 2024. All rights reserved.