Sending Email Notifications After User Signup In WordPress, With Delay!

A while back I had a project that needed an email to get sent out to users after a specific action was performed, but it had to be delayed a defined number of days, and as the project was already a custom coded theme, it felt that implementing something custom to solve that problem was the right approach.

In the next example, I will outline how exactly I implemented sending a “continue signup/additional user information” 2 days after the user has officially been registered on the website.

To accomplish the above, we need to use a default user field “user_registered” – this field is generated by default when a user record is created in WordPress, and also we need a meta field to mark a user as “notified” for the current notification.

The process to send out this email is:

  • 1) Get a list of users that we need to send out the notification. (Fetch with SQL Query)
  • 2) Iterate over the returned list of users, and send out the email.
<?php 
global $wpdb; 

// * defines the meta field name that we will be adding to users
$meta_notified = '_user_notified';

$sql = "
           SELECT ID FROM {$wpdb->users} as usr
           LEFT JOIN {$wpdb->usermeta} as meta_notified on (usr.ID = meta_notified.user_id AND meta_notified.meta_key = '{$meta_notified}')
           WHERE meta_notified.meta_value IS NULL
           AND NOW() >= DATE_ADD(usr.user_registered, INTERVAL 2 DAY)
           AND NOW() < DATE_ADD(usr.user_registered, INTERVAL 5 DAY)
           ";

?>

To explain in short what is happening in the above SQL statement:

  • on Line 3: We define the the name of the user meta we are going to use for marking the user as “notified”.
  • on Line 6: we are joining the meta field to the query to exclude notified users.
  • on Line 9 and 10: we target a range of results based on the register date.

Now the existing part, making WordPress send out the emails,

<?php 
// * execute the SQL and get result list
$list_of_users = $wpdb->get_results($sql);

if(!empty($list_of_users)) {

	foreach($list_of_users as $user) {
	        
                // * fetches the user object
		$user_data = get_user($user->ID);
	 
		// * send out the email

		// * email contents 
		ob_start(); ?>

		You can click <a href="http://examplewp.com/continue-registration">this link here</a> to finish your sign up process.<br>

		Thanks,
		WP Team

		<?php 
		$email_body = ob_get_clean();		

		$to = $user_data->user_email;
		$subject =  'Hello '.$user_data->display_name.' Continue Signup';
		$body = $email_body;
		$headers = array('Content-Type: text/html; charset=UTF-8','From: WordPress <[email protected]');

		wp_mail( $to, $subject, $body, $headers );
			

		// * mark the user as already notified
		update_user_meta($user->ID,$meta_notified ,1);

	}
 }
?>

As you can see from the snippet above, its a pretty simple bit of code, but it gets the job done, and prevent duplicate emails getting sent to users.

Leave a Reply

Your email address will not be published. Required fields are marked *