Skip Navigation

[Resolved] send email to repeat field after cred submit

This support ticket is created 6 years, 10 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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 16 replies, has 2 voices.

Last updated by Nigel 5 years ago.

Assisted by: Nigel.

Author
Posts
#623142
diagram.jpg
explain01.png

Hi there!

Tell us what you are trying to do?

Using my custom post called Laboratorio I want to send one notification to multiple recipients when CRED submit the form. I have managed to select the "laboratory" inside the user's profile to create that relationship, that's ok.

As you previous know on the last ticket:

I have a custom post called "laboratorio". Inside that custom post I have two fields. One single field called name, and other email field. Email Field is a repeting field.

After that I make a function who returns de array of the emails. That function get's the current user id, the key and gets the User meta, then With that info we get the post_meta and works

I checked the function works because manually I changed the return like return $valoremail[0] (and shows the first email) and $valoremail[1] shows the second one.

Here is:

// Add Shortcode
function averiguaremailasociado() {

	 	$user_id = get_current_user_id();
  //this is our types user field name
  		$typesuserfield = 'wpcf-labasociadoemail';
  		$single = true;
  //this is our repeating email field inside "laboratorio" custom post type
        $ctrepeatingfield = 'wpcf-emaillabasociado';
  
		$user_last= get_user_meta($user_id, $typesuserfield, $single );
  
  //using false on the last position we are obtaining an array of the post meta
		$valoremail = get_post_meta ( $user_last, $ctrepeatingfield, false);
//return the array 
return $valoremail;

}
add_shortcode( 'devolveremailasociado', 'averiguaremailasociado' );

-------------------------------------
CRED PART
-------------------------------------
In my CRED form I added this

 
 [cred_field field='campoemailnotificar' post='upn' value='[devolveremailasociado]' urlparam='' readonly='false' class='form-control' output='bootstrap']

Inside upn, I created a field called "campoemailnotificar" (email type) in order to use "Send notification to an email specified in a form field:"

So, value='[devolveremailasociado]' , devolveremailasociado is a shortcode who prints out the array as you see on the code I put.

Using this doesn't work So I tried to add some code to overwrite $recipients but still doesn't work

Is there any documentation that you are following?
Read a lot of tickets before, in particular this one: https://toolset.com/forums/topic/send-cred-form-notification-to-multiple-email-addresses/

See attached images. Hope all is clear

Regards

#623218

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Rafael

This won't work as-is for several reasons.

Firstly, shortcodes should return strings, not arrays.

You could modify your shortcode so that it returned the email addresses as a comma-separated list, but that wouldn't actually help.

I tested adding multiple email addresses to one email field that is used for CRED notifications and it simply didn't work, the emails weren't sent (or, at least, the addressee fields were not correctly formed so the sent emails did not arrive).

Also, while testing I noticed what appears to be a bug, namely that if you use the value attribute of an email field in the CRED form you cannot then select that field as your email recipient. (I have reported that bug.)

Now, you can still achieve what you want, but via a slightly different route.

The CRED API has a filter to modify notification recipients, see: https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

I haven't looked too closely at your actual code, but you need to move the logic from a shortcode into a function hooked to that filter.

There is an example in the documentation.

If you get stuck let me know.

#623246

Hi Nigel

Indeed, I also tried to return the email addresses as a comma-separated list but in my testing I see didn't work too

I tried some code using cred_notification_recipients like this but didn't work. Can you take a look please?

add_filter('cred_notification_recipients', 'my_cred_notification_recipients_func', 10, 4);
function averiguaremailasociado() {

	 	$user_id = get_current_user_id();
  //this is our types user field name
  		$typesuserfield = 'wpcf-labasociadoemail';
  		$single = true;
  //this is our repeating email field inside "laboratorio" custom post type
        $ctrepeatingfield = 'wpcf-emaillabasociado';
  
		$user_last= get_user_meta($user_id, $typesuserfield, $single );
  
  //using false on the last position we are obtaining an array of the post meta
		$valoremail = get_post_meta ( $user_last, $ctrepeatingfield, false);

  
  
    if($form_id == 79){ //replace it with your CRED form ID
      $recipients[] = $valoremail;
    }
    return $recipients;
}
#623268

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Rafael

Before looking at your code in detail, if this is actually what you have used then it won't work. In your line that adds the filter the callback function is 'my_cred_notification_recipients_func' but your actual function is called 'averiguaremailasociado'.

Also you need to pass the filter arguments (the first argument is required and must be returned, the others are optional but likely helpful).

I haven't made any changes of note to your code, only fixed these, which are a minimum for your code to have a chance of working:

add_filter('cred_notification_recipients', 'averiguaremailasociado', 10, 4);
function averiguaremailasociado($recipients, $notification, $form_id, $post_id) {
 
        $user_id = get_current_user_id();
	  //this is our types user field name
        $typesuserfield = 'wpcf-labasociadoemail';
        $single = true;
  //this is our repeating email field inside "laboratorio" custom post type
        $ctrepeatingfield = 'wpcf-emaillabasociado';
   
        $user_last= get_user_meta($user_id, $typesuserfield, $single );
   
  //using false on the last position we are obtaining an array of the post meta
        $valoremail = get_post_meta ( $user_last, $ctrepeatingfield, false);
 
   
   
    if($form_id == 79){ //replace it with your CRED form ID
      $recipients[] = $valoremail;
    }
    return $recipients;
}
#623292
array.jpg

Hi Again Nigel,

Thanks for fixing this.

I activate in the CRED form the option " Send notification to a specific email address:". That email arrives correctly. However doesn't send any email to the other recipients. Is like the function isn't triggered.

I know for sure that $valoremail has the array with the email address because I already checked as i explained before

I also make a print_r of the above code, to print the value, just to be sure and as you see in the screenshot the array is working ok (this array was printed on the cred_notification_recipients filter)

Can you provide me a private reply to take a look please? I know there is a bug as you see but I need and alternative solution

Regards

#623321

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sure, let me mark your next reply as private and I will try and see what is preventing this from working.

Where have you added your custom code?

What page is the CRED form on?

#623351

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Rafael

I can't find your debug logs.

Your wp-config.php is set up to create logs in the root directory in a file error_log.txt but that is missing, and so I edited your wp-config.php file to create the normal WordPress debug.log file in wp-content, and that is not being generated either.

Where should I expect to find them?

#623366

You are right. There is no error_log. I am trying to force php to generate and error and generate a error_log. Give me a few minutes

#623368

Is on the logs folders. The file is called "platafolma_org.php.error.log"

Just go to the root and enter inside "logs" folder.

Regards

#623379

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Rafael

I think you can see the problem then in the log where I sent the value of $recipients after you had appended the new email addresses:

deleted

The email addresses you add need to be in the same format as the existing email address, i.e. an array with elements for address, to, name, and lastname.

#623408
array.png

You are right. However I hard coded and array (just to do a quick test) and the log is showing OK (same format) but still doesn't work. It only sends to the first email. I used my own personal email instead of gmail, just to be sure

In Theory, it should work but it doesn't.

#623444

Hi Nigel, I found an easy solution that works. I want to thank you again your good work on the ticket.

Working back with my shortcode function I added a this:

$valueswithcomas = implode(",", $arrayemail);
return $valueswithcomas ;

This have all the email address in a single string.

Then in the Cred Form, I add a cred_generic_field with persist:1 like this:

[cred_generic_field field='sender' type='email' class='' urlparam='']
{
"required":0,
"validate_format":0,
"persist":1,
"default":"[mycustomshortcode]"
}
[/cred_generic_field]

Finally on "Send notification to an email specified in a form field:" I set the cred_generic-field but added as "CC" and not as "To", this is really Important.
Also you will need to activate the option Send notification to a specific email address (this will be the "To").

Regards

#623593

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Rafael

Thanks for sharing that. I had thought about using a generic field instead of an email field but hadn't got around to testing it yet.

#624022

Thanks for all 🙂

#624746

I see there are some emails in plain text and I am getting a lot of SPAM. Please, can delete the entire post? Or at least the part when I show emails?

Regards