Skip Navigation

[Resolved] Trying to send an email to an email id in a custom field

This support ticket is created 3 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
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: Africa/Casablanca (GMT+01:00)

This topic contains 6 replies, has 2 voices.

Last updated by Jamal 3 years, 10 months ago.

Assisted by: Jamal.

Author
Posts
#1660009

Tell us what you are trying to do? I am using forminator pro that is a plugin from WPMUDEV and it sends email from the form to an admin email id but they are trying to help me send it to a custom field created in Toolset.. here's a link to support ticket.. hidden link .. and here's the page that I am trying to use it on.. hidden link

are we on the right track?

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#1661105

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello and thank you for contacting the Toolset support.

I assume you are talking about the code on this reply hidden link right?

I see possible errors:
- The value returned from types_render_field may be formatted, you may need to use the argument "output=raw" to get the raw value, check the documentation here https://toolset.com/documentation/customizing-sites-using-php/functions/
- The value returned is not an email and must not get added to "$recipients", you may need to use get_userdata($user_id)->user_email, check the documentation of this function here https://developer.wordpress.org/reference/functions/get_userdata/
- The value returned has probably multiple values separated by (,), you may need to explode it.

For the first point, you may also use the default WordPress function "get_post_meta" and prefixing the slug with "wpcf-", that's how Toolset stores the custom fields created by Toolset, check this article https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/
https://developer.wordpress.org/reference/functions/get_post_meta/

Check this sample code and please note that it needs testing:

add_filter( 'forminator_get_admin_email_recipients', function( $recipients ) {
	global $post;

	if ( function_exists( 'types_render_field' ) ) {
		$custom_ids = get_post_meta( 'wpcf-notification-email', true );
		// if multiple emails explode them
		$custom_ids = explode(',', $custom_ids)
		if ( count( $custom_emails ) > 0 ) {
			unset( $recipients );
			foreach ($custom_ids, $custom_id)
			$recipients[] = get_userdata($custom_id)->user_email;
		}
	}

	return $recipients;
} );

I hope this helps. Let me know what you will get.

#1662779

Hi Jamal

Thanks for the update.. I have forwarded this to the WPMU guys and I'll ask them to test and decide..

Thanks once again.. will wait for their response and if they have some queries will get back to you.. if not, will close this ticket as resolved asap.

Have a nice day..:-)..

Regards,
Alim

#1664249
Screenshot 2020-06-16 12.33.14.png

Hi Jamal

Thanks for the update.. :-)..

Have forwarded the code to WPMU team and they confirmed it was all good..

But when I tested with multiple email ids it didn't seem to work.. I made the email custom field as 'Allow to add multiple instances' .. that's fine.. right? (screenshot attached)

Please do check the ticket at this URL to see if you can help the WPMU team

hidden link

Regards,
Alim

#1664733

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello Alim and thank you for your feedback. As the field stores emails instead of user IDs, we can omit the use of get_userdata/. I have also a mistake in the get_post_meta, it should take the post_id as the first argument and false as 3rd argument to return an array. We can use the $post for post_id, but I am not sure, maybe the WPMU team can help us get the ID of the created post.

The sample code will be:

add_filter( 'forminator_get_admin_email_recipients', function( $recipients ) {
    global $post;
 
    // get an array of emails from the custom field
	$custom_emails = get_post_meta( $post->ID, 'wpcf-notification-email', false ); // <= change $post->ID with the ID of the created post.
	
	if ( count( $custom_emails ) > 0 ) {
		unset( $recipients );
		foreach ($custom_emails as $custom_email) {
			$recipients[] = $custom_email;
		}
	}
 
    return $recipients;
} );

I'll remain at your disposal.

#1668497

Hi Jamal

Thanks for the update..:-)..

The team at WPMU has used your suggestion and used this final code

add_filter(
        'forminator_get_admin_email_recipients',
        function( $recipients ) {
                if ( function_exists( 'types_render_field' ) ) {
                        $custom_emails = get_post_meta( $_POST['page_id'], 'wpcf-optician-notification-email', false );

                        if ( count( $custom_emails ) > 0 ) {
                                unset( $recipients );

                                foreach ( $custom_emails as $recipient ) {
                                        $recipients[] = trim( $recipient );
                                }
                        }
                }

                return $recipients;
        }
);

And it seems to be working fine..:-)..

Please do check and confirm the above if it's all ok and I'll close the ticket

Regards,
Alim

#1668617

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello Alim,

The code looks good to me. But I'll need to test it to be sure. If you confirm that it is working for you, that's the test I'll need to do 🙂

Let me know if you still need help with anything.

Best regards,
Jamal

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