0

I have writed a lugin that will send emails after the user completed a custom registration form. I need to send emails after 24 hours from registration, is this possible by using the wp mail function?

UPDATE

Not sure if this can work as expected but I've added as suggested a custom action hooc into the construct of my plugin class in this way

add_action('send_login_credential', array($this, 'send_scheduled_registration_email'));

And into the function that will manage the ause registration approval, I've added the code for the cron in this way

                $user_email = $customer->user_email;
                $num_t = sanitize_text_field( $request->get_param('numTessera') );
                $subject =  'User approved';
                $message = ".... \n";
                $message .= "...: $num_t \n";
                $message .= "...password: \n";
                $message .= "$password \n";              
                //
                wp_schedule_single_event( DAY_IN_SECONDS * 1, 'send_login_credential', array( $user_email, $subject, $message ));

the function that will be called from the custom action hook will only contain the logic to send the email

    public function send_scheduled_registration_email( $user_email, $subject, $message )
    {
        //add_action('send_login_credential', array($this, 'send_scheduled_registration_email'));

        $message_sent = wp_mail( $user_email, $subject, $message );
        if( $message_sent ){
            return [
                'status' => 'ok'
            ];
        }
    }

Is there something I will need to do in the code or it can work?

1
  • Yes that looks right to me at first glance.
    – Rup
    Oct 24 at 10:08

2 Answers 2

1

You can't schedule a future email from wp_mail * but you can use wp_schedule_single_event to run in 24 hours time, and write an action hook handler that calls wp_mail() to send your mail, e.g. something like

wp_schedule_single_event( time() + (60*60*24), 'send_new_user_next_day_email', args( $userId ) );

function send_new_user_next_day_email( $userId ) {
    // look up the user and call wp_mail()
}
add_action( 'send_new_user_next_day_email', 'send_new_user_next_day_email', 10, 1 );

Note that this runs on the WordPress cron system and so will trigger when you next get traffic on the site after the 24 hours are up. You can however set up system scheduler events to trigger cron events even when there's no website traffic - see the wordpress.org cron docs.


* unless the mail server you're sending through supports it, e.g. Office 365 does, but I'm not sure what mail headers etc. you'd need to set up to do that.

2
  • I will give to the function a try. Mainly I will need that the email is scheduled to be sent after 24 hours indipendently from the site traffic. Probably as you've suggested I will need to use the wiordpress cron, if I understand well it's the same of the function you've suggested?
    – ICTDEV
    Oct 23 at 7:19
  • 1
    wp_schedule_single_event uses the cron system, yes - it queues up jobs for the cron system to run. If you don't have enough traffic you can set up a scheduled job on your server to call the /wp-cron.php endpoint on your site which will trigger any scheduled jobs then.
    – Rup
    Oct 24 at 10:10
1

To achieve this, you'd first set up a scheduled task using the wp_schedule_single_event function after the user completes the registration. This will trigger an action 24 hours later, and in that action, you can use wp_mail to send the desired email.

Here's a basic outline to give you an idea:

// Let's say this is your registration function
function your_custom_registration_function($user_email) {
    // ... your registration code ...

    // Schedule the email to be sent in 24 hours
    wp_schedule_single_event(time() + 24*60*60, 'send_followup_email', array($user_email));
}

add_action('send_followup_email', 'send_email_function');

function send_email_function($user_email) {
    $subject = "Hey! It's been a day since you registered!";
    $message = "We're so glad you joined us! Here are some things you might want to check out...";
    wp_mail($user_email, $subject, $message);
}
1
  • I0m looking at wp cron, but my doubt is that the scheduled actions will be triggered only if the site have traffic. In my case this can be a problem because users will be subject to approval from a custom role I have, in that case I will need to schedule the email sen
    – ICTDEV
    Oct 23 at 8:10

Your Answer

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

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