Skip Navigation

[Resolved] How do I send a notification email to an email address that is a Custom Field?

This support ticket is created 4 years 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 9 replies, has 2 voices.

Last updated by michielM 4 years ago.

Assisted by: Raja Mohammed.

Author
Posts
#1840089

Tell us what you are trying to do?
I want to send a notification to an email address of a type that was dynamically chosen from a list of Custom Type objects.

In my example, I have an overview of Ambassadeurs (ambassadors) and I can contact each of them thru a form (see link below). The form is filled if you get to the form via the Ambassadeur profile page. All Ambassadeurs have a field "emailaddress". How do I send an email to that emailaddress?

What is the link to your site?
hidden link

#1841231

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello there,

The cred_notification_recipients filter will come handy to add additional email address to the notification,

Reference : https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

In the example below I am assuming the Ambassador drop down gives the post id, if not you might have to get the post id from the title and use get_post_meta to get the appropriate custom field.

add_filter('cred_notification_recipients', 'modify_recipients', 30, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {
     // Check notification name matches target notification
     
    if ( isset($notification['name']) && 'pageform' == $notification['name'] ) { // replace with notification name
		 // Assuming the custom field where the Ambasdor is selected returns the id
		 // if not use get_page_by_title to get the id of post https://developer.wordpress.org/reference/functions/get_page_by_title/
		 
		 
		// get the custom field , wpcf- if the custom field is added by types
		$email = get_post_meta($_POST['page_select'],'wpcf-email',true);
	 
 
        // Add a BCC to  
        $recipients[] = array(
            'to'        =>  'bcc',
            'address'   => $email,
            'name'      =>  '',
            'lastname'  =>  ''
            );
    }
          
    return $recipients;
}

I hope this helps better

Regards
Raja

#1841275

Hi Raja,

Thank you so much for this!

One more issue: how do I know if the the Ambassador drop down gives the post id? The field in the form is defined as follows:

		<label>Which ambassador?</label>
		[cred_field field='@rel-aanvraag-ambassadeur.parent' class='form-control' output='bootstrap' select_text='--- not set ---' required='false']

FYI: this form will create a Custom Type 'Request ambassador' which is/has a relationship between Requests (aanvraag in Dutch) and Ambassador (ambassadeur): Ambassador 0-1 ... 0-* Request

#1841367

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I see the code just adds a Select field from the post relationship However i am not sure how the actual select field is build in the custom type did you add any custom code to dynamically generate the select values or it is manually added in the Field group ?

Inspecting the page source i can see the select option value hold the ambassador id refer hidden link 🙂

#1841857

Okay, so I have now the code to find the proper Ambassador with email address and name:

add_filter('cred_notification_recipients', 'modify_recipients', 30, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {

     // Check notification name matches target notification
	if ( isset($notification['name']) && 'Mail naar ambassadeur' == $notification['name'] ) { // replace with notification name
		// get the custom field , wpcf- if the custom field is added by types
		$email = get_post_meta( $_POST['@rel-aanvraag-ambassadeur_parent'], 'wpcf-gastdocent-email', true );
		$name = get_post_meta( $_POST['@rel-aanvraag-ambassadeur_parent'], 'wpcf-gastdocent-naam', true );

        // Add a BCC to  
        $recipients[] = array(
            'to'        =>  'bcc',
            'address'   =>  $email,
            'name'      =>  $name,
            'lastname'  =>  ''
            );
    }

	echo "<pre>";
	print_r($email);
	print_r($name);
	print_r($recipients);
	echo "</pre>";

	return $recipients;
}

This outputs:

ambassador@email.com
Michiel Meijers
Array
(
    [0] => Array
        (
            [to] => to
            [address] => request@email.com
            [name] => Michiel test 12 nov 0941
            [lastname] => 
        )

    [1] => Array
        (
            [address] => admin@email.com
            [to] => 
            [name] => 
            [lastname] => 
        )

    [2] => Array
        (
            [to] => bcc
            [address] => ambassador@email.com
            [name] => Michiel Meijers
            [lastname] => 
        )
)

The only problem now is that I don't receive an email on the ambassador@email.com account.

#1841861

Strange. When I change 'bcc' to 'to' it works. Any thoughts?

$recipients[] = array(
            'to'        =>  'to',
            'address'   =>  $email,
            'name'      =>  $name,
            'lastname'  =>  ''
            );

But I don't need bcc anyway, so thank you!

#1841863

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Have you tested this without the debug codes ?

echo "<pre>";
    print_r($email);
    print_r($name);
    print_r($recipients);
    echo "</pre>";

,
Since displaying an output before the return statement will probably prevent the return value from filters.

#1841869
Screenshot 2020-11-12 at 09.59.02.png

Hmmm, when I try to add a bcc in the form notification itself, it also stops working. Am I doing this right?

#1841881

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Screenshot 2020-11-12 at 2.36.20 PM.jpg

May be an restriction on your mail server or SMTP setup. Please install a mail logger plugin like https://wordpress.org/plugins/post-smtp/ once installed, Initiate an mail, check the logs from Post SMTP > EMail logs, open the recent log look out for Bcc in the mail header as in the screenshot.

If you can see them, yet it is not delivered then it is something you might need to check with your hosting provider or the mail service if any used on your site.

#1841961

I'll look into the mailing problem. My initial issue is resolved now. Thank you!