Skip Navigation

[Resolved] conditional send email

This support ticket is created 7 years, 4 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 6 replies, has 2 voices.

Last updated by oliverD 7 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#546759

I am trying to:
have a checkbox that only appear for administrators on a user update form(which I have done). when this checkbox is ticked and email is send to the user otherwise no email. If a user that is not administrator updates the profile an email should be sent.

I visited this URL:

I expected to see:
I was trying to use a generic field but the value was not being passed back. I then added a new field which worked by the value is saved and therefore whatever option that last administrator checked will drive whether an update is send for any user updating the profile that is not a member of the administrator group

Instead, I got:

#546810

Dear Oliver,

There isn't such a built-in feature within CRED plugin, as a workaround, you can try with CRED action hook "cred_mail_header" to change the notification to someone else, for example:
Add below codes into your theme/functions.php:

function conditional_send_email_func( $headers, $formid, $postid, $notification_name, $notification_number ) {
    if($formid == 123 && $notification_name == 'notification1'){
		if(!isset($_POST['send-mail']) || $_POST['send-mail'] != 1){
			$headers = false;
		}
	}
    return $headers;
}
add_filter('cred_mail_header', 'conditional_send_email_func', 10, 5);

function conditional_send_email_func2($atts){
	if(!$atts['headers']){
		$atts['to'] = 'no-reply@123.com'; // here setup an email address
	}
	return $atts;
}
add_filter('wp_mail', 'conditional_send_email_func2');

Please replace 123 with the CRED form ID, and replace "notification1" with the notification name, replace the email address "no-reply@123.com" with your own email address.

#547108

thanks, this looks like an option that could work. I have a couple of questions.
1. Will this approach bypass the email notification at the end of the cred form or simply replace the "to" email address when the checkbox is checked?
2. How could I input the email of the user profile that is currently being edited?

#547181

1) simply replace the "to" email address when the checkbox is checked

2) You can try the action hook "cred_mail_header" as the example in our document:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_mail_header
click "cred_mail_header", there is an example:

/The following example shows how you can use any value submitted by the form for updating the $headers.
//In the following snippet, we first get the user by ID and then add this user's email address to a custom variable ($myheaders).
//Finally, we merge our custom variable with the $headers.
#547246

thanks again.

one final question in the function conditional_send_email_func2($atts) how can i refer to the email field "hf-email" which is a field in the form?

#547267

Yes, it is possible, you will need to pass the email address from function 1 to function 2, for example,
1) modify the PHP codes to:

function conditional_send_email_func( $headers, $formid, $postid, $notification_name, $notification_number ) {
    if($formid == 123 && $notification_name == 'notification1'){
        if(!isset($_POST['send-mail']) || $_POST['send-mail'] != 1){
            $headers['send-mail'] = 0;
            $other_mail = get_user_meta($postid, 'wpcf-hf-email', true);
            $headers['other_mail'] = $other_mail;
        }
    }
    return $headers;
}
add_filter('cred_mail_header', 'conditional_send_email_func', 10, 5);
 
function conditional_send_email_func2($atts){
    if(isset($atts['headers']['send-mail']) && $atts['headers']['send-mail'] == 0 && isset($atts['headers']['other_mail'])){
        $atts['to'] = $atts['headers']['other_mail']; // here setup an email address
		unset($atts['headers']['send-mail']);
		unset($atts['headers']['other_mail']);
    }
    return $atts;
}
add_filter('wp_mail', 'conditional_send_email_func2');

2) Add a generic checkbox field "send-mail" in your CRED form:

	[cred_generic_field field='send-mail' type='checkbox' class='' urlparam='']

It will output a checkbox field in your CRED form, and pass URL parameter "send-mail"
Please replace 123 with the CRED form ID,
replace "notification1" with the notification name,
replace "wpcf-hf-email" with custom user field "hf-email" slug

#547763

Brilliant solution. Thank you very much.