Whenever a user performs an action involving sending out email notifications, it takes a long time to process and therefore the user must wait a few seconds until the resulting page is loaded.

As a solution I wanted to do the emailing asynchronously.

To do this, I used wp_schedule_single_event and I set the time parameter to time() (ie: immediate).

This works well, however, if back to back emails are sent, they don't all get sent. I don't understand why. The docs mention:

Attempts to schedule an event after an event of the same name and $args will also be ignored.

But my $args are not the same.

Could the problem have something to do with:

The action will fire off when someone visits your WordPress site, if the schedule time has passed.

since a post might be made before someone has had a chance to visit the site and trigger the previous post's task?

Is this the preferred way of achieving asynchronous emailing?

Thank you.

(aside: I will have a load balancer pinging my site regularly. Will this be enough to trigger the scheduled actions if they have passed their scheduled time?)

The code: The Util functions:

     public static function send_asynch_email( $email, $subject, $message, $headers )
        wp_schedule_single_event( time(), 'asynch_email', array($email, $subject, $message, $headers));

    public static function send_email( $to, $subject, $message, $headers = '', $attachments = array() )
        return wp_mail( $to, $subject, $message, $headers );

And in my functions.php file:

add_action('asynch_email', 'trigger_asynch_email', 10, 4);
function trigger_asynch_email( $email, $subject, $message, $headers ){
    Util::send_email($email, $subject, $message, $headers);
  • Do you mind adding some code examples for us? – stims Oct 14 '16 at 18:00
  • I doubt that a simple IP ping will do it. Maybe a separate CRON job at your host, which references a page at your site: * * * * * wget -O - http://mywebsite.com/myaction.php/ >/dev/null 2>&1 (runs once every minute, you can change of course...) – C C Oct 14 '16 at 18:21
  • 1
    can not really help without code ! – Benoti Oct 14 '16 at 19:31
  • updated the question with code. – theyuv Oct 15 '16 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.