1

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: <[email protected]>');
        }
    }
}

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?

4
  • 1
    Have you searched for "Newsletter Plugins"?
    – Howdy_McGee
    Oct 31, 2013 at 20:45
  • 1
    Careful, if you start sending out a ton of unsolicited mail from your domain you're going to get blacklisted. You're much better off using a service like aweber, mailchimp, etc. Oct 31, 2013 at 22:13
  • Yes @Howdy_McGee i searched but the plugins send emails when a new post or a new page is published.The problem is that i've created a section called "events" and it's neither post or page it simply uses $wpdb thats why I need a function to do that :/ Nov 1, 2013 at 8:26
  • What newsletter plugin does what i want do you know any? Nov 1, 2013 at 8:31

1 Answer 1

2

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 ...).

3
  • 1
    if there is no personalization done in the mail you might be able to save time by sending it to multiple recipients by using BCC. Nov 1, 2013 at 5:00
  • I used Wysija newsletter but i don't know how to send emails "programmatically" when i call my own function send_emails().That is what I want can I do that with Wysija or any other newsletter? Nov 1, 2013 at 8:47
  • 1
    @AntonioPapa Actually Wysija already adds jobs to wp-cron itself to shedule the emails in batches (like 50 mails per cron run), so you might have a look at that, maybe you are able to create those cronjobs by yourself to make it send emails at regular intervals (wordpress.org/plugins/wp-crontrol is a great plugin for looking which jobs are sheduled).
    – s1lv3r
    Nov 1, 2013 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.