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

时间:2017-12-21 04:20:10

标签: php wordpress email

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

//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<info@mysite.com>\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();

2 个答案:

答案 0 :(得分:2)

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

$users = get_users();

foreach( $users as $user ) {

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

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

希望有所帮助:)

答案 1 :(得分: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<info@mysite.com>\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();