I’ve created this function:
function email_subscribed_users($event)
{
$users = get_users('role=subscriber');
foreach ($users as $user) {
$accepted = get_user_meta($user->ID, 'accepted', true);
if ($accepted) {
wp_mail($recipients, $event->event_title, $event->event_desc, 'From: <example@example.com>');
}
}
}
But it requires a lot of time sending the emails knowing that the users will increase by time.What function should I use to send the emails efficiently or there is a newsletter plugin to do this?
You can directly query for users having a specific user_meta value. This will gain some performance over loading all the users first and then iterating over them and loading the meta_value afterwards.
Have a look at the WP_User_Query class.
If we are speaking of larger numbers of mails you may be better off to use a full-blown newsletter plugin (that will automatically queue the mails and not try to send them all at once). And if you are really sending a lot of mails (like +5.000 in one newsletter) I would advise you to use an external mail service provider (CakeMail, Mailchimp …).