Skip Navigation

[Resolved] Send email notification to all users of a certain role

This support ticket is created 7 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 2 voices.

Last updated by nabils 7 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#400583

Hi,
I created a custom user field "receive_notification" where the user can set "yes" if he wants to receive notifications and "no" otherwise.
Now, in a CRED POST form that create an advertisement, I want to send an email notification to all users who have set "receive_notification" to "yes" in their profiles.
https://toolset.com/forums/topic/send-email-notification-to-user/ solved a problem similar to mine but I do not want to show the generic field and I have a custom user field instead of "role". Is it the right way to solve my problem? Shall I still use the generic field but hide it?
add_shortcode('get-all-users', 'get_all_users_data');
function get_all_users_data($attr) {
global $post;

if(isset($attr['role']) and $attr['role']!='')
$role = $attr['role'];
else
$role = 'administrator';
$blogusers = get_users( 'orderby=user_id&role='.$role);

$temp = array();
foreach ( $blogusers as $user ) {
$temp[]= '{"value":"'.$user->ID.'","label":"'.$user->user_email.'"}';
}
$temp = join(',',$temp);
$str = '['.$temp.']';

return $str;
}

Your help is appreciated.

#400733

Dear Nabils,

In your case, I suggest you try with CRED filter hook "cred_notification_recipients", it does not need any generic field,
For example:
The custom user field "receive_notification" is created with Types plugin, in database the field meta_key is "wpcf-receive_notification", when user enabled the field, it stores value "1" into database, then you can modify the PHP codes as below:

add_filter('cred_notification_recipients', 'send_subscribed_users_func', 10, 4);
function send_subscribed_users_func($recipients, $notification, $form_id, $post_id){
    if ( 123 == $form_id){ //if it is specific CRED form
		$args = array(
			'meta_key'=> 'wpcf-receive_notification',
			'meta_value' => 1,
			'fields' => array( 'display_name', 'user_email')
		);
		//get all users with "receive_notification" field
        $users = get_users( $args );
		//add them as recipients
		foreach($users as $user){
			$recipients[] = array(
				'address' => $user->user_email,
				'name' => $user->display_name
			);
		}
    }
    return $recipients;
}

Please replace 123 with your CRED form ID.

More help:
https://developer.wordpress.org/reference/functions/get_users/

#401105

Dear Luo,
I tried this but did not get any email.
Log error shows:
PHP Notice: PHP Notice: Undefined index: to in /home1/capitbq3/public_html/tadreeboman/wp-content/plugins/cred-frontend-editor/embedded/classes/Notification_Manager.php on line 1110
[24-May-2016 08:16:50 UTC] PHP Notice: Undefined index: lastname in /home1/capitbq3/public_html/tadreeboman/wp-content/plugins/cred-frontend-editor/embedded/classes/Notification_Manager.php on line 1121

Actually, the code will define the recipients, but we have also to define the notification in the CRED where we have to define the subject, body... I tried this but I am not sure which option I should set for "Where to send this notification:"? Send notification to a specific email address:?

#401399

Are you going to do this?
Send the Notification email to the wordpress users, whose user filed "receive_notification" is set, and different subject and body for each user, for example:
To user 1, subject 1, body 1
To user 2, subject 2, body 2
...

This is not possible within CRED form, I suggest you check out our certified partners for it:
https://toolset.com/consultant/

If you only need send same email to wordpress users, whose user filed "receive_notification" is set,
You can modify the PHP codes as below:

add_filter('cred_notification_recipients', 'send_subscribed_users_func', 10, 4);
function send_subscribed_users_func($recipients, $notification, $form_id, $post_id){
    if ( 123 == $form_id){ //if it is specific CRED form
        $args = array(
            'meta_key'=> 'wpcf-receive_notification',
            'meta_value' => 1,
            'fields' => array( 'display_name', 'user_email')
        );
        //get all users with "receive_notification" field
        $users = get_users( $args );
        //add them as recipients
        foreach($users as $user){
            $recipients[] = array(
				'to'	=> 'to',
                'address' => $user->user_email,
                'display_name' => $user->name
            );
        }
    }
    return $recipients;
}

Please replace 123 with your CRED form ID.

#401417

Thank you Luo,
Your help is most appreciated.
Actually, I want to send the same email to all users. I tried your code and it works, thank you. However, I need "receive_notification" to be checkboxes (not a select) and the email should be sent if the user has checked option "option1" of the checkboxes type. I tried to update your code, but this checkoxes is very tricky and could not find a similar example in the net.

#401424

HI
Sorry, maybe no need to bother with this checkboxes (you can post the solution for other users if you want). I used multiple instances of the checkbox type instead and it does the job.

Thank you

#401466

OK, please let me know if you need other assistance. thanks

#401471

No, thank you very much!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.