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?
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;
}
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
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 🙂
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.