I think the very simplest here is to use Custom Code.
Note, Many To Many Relationships can only be set with Relationship Forms in the front end but those cannot have Notifications or API calls, so you can NOT assign your messages with a Toolset Form to the right Agency without custom code, since you will have to use a Post Form (given you want a notification)
The Relationships can be set with the Toolset Relationships API explained here:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
The emails instead will be sent with https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients
Now, to proceed:
1. Create your relationship and agencies, and a email field at least for Agencies
2. List them as you do in your View passing the Post ID in the URL (?post_ids[]=7&post_ids[]=8&post_ids[]=10, etc)
3. Create a POST Form that creates new Messages
4. Attach one notification to that form, that sends an email to whatever recipient you want with the content you want (for example, you could sent it to your self, or just any dummy email you chose)
5. Insert this Form on the View (but outside the loop) where you display the agencies compared.
6. When the User now will submit this form with all details, it will send to the dummy email, but we are going to hook a custom code that will make sure it also sends it to the emails of the agencies listed in that View
This code is complex and requires custom coding, but I can make a full example here.
You will need to get the URL parameters so to get the IDs of the agencies you want to send the email to.
That's done with
$url_params = $_SERVER['QUERY_STRING'];
parse_str($url_params, $get_array);//this gives us all the URL parameters in $get_array
This gives us an array of IDs from the post_ids URL parameter (the IDs of the agencies)
From there you will need to get the Emails of those agencies, and that can be done with get_post_meta, since you have the email in a custom field.
You will want to do that foreach of the posts.
foreach ($get_array['post_ids'] as $key => $value) {
$email_of_agency = get_post_meta($value, 'wpcf-email', true);//Change the Email Field slug as adequate
}
Now we have all the emails that we should address.
All we need to do now is use the Forms API to update the "To" of the notification, as seen here https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients
We still do that within our foreach, so it's done for each of the emails:
foreach ($get_array['post_ids'] as $key => $value) {
$email_of_agency = get_post_meta($value, 'wpcf-email', true);
$recipients[] = array(
'to' => 'bcc',
'address' => $email_of_agency,
'name' => '',
'lastname' => ''
);
}
All put together into a Toolset Forms API code, that will be put in your theme's Functions File, looks like this:
add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {
// Check notification name matches target notification
$url_params = $_SERVER['QUERY_STRING'];
parse_str($url_params, $get_array);
foreach ($get_array['post_ids'] as $key => $value) {
$email_of_agency = get_post_meta($value, 'wpcf-email', true);
$recipients[] = array(
'to' => 'bcc',
'address' => $email_of_agency,
'name' => '',
'lastname' => ''
);
}
error_log(print_r($recipients, true));
return $recipients;
}
This code is tested locally and does send the email to:
- our dummy email we set in the Notification of the Form itself
- and bcc's each of the new emails passed in the above code
Of course you will need to adapt that code to your site, and eventually you will want to use a to, instead of bcc, which is possible by altering the adequate aspects of the code as our DOC explains.
If you require help with further customization of the code, our Contractors would be the right place to ask:
https://toolset.com/contractors/
Related to connecting the message to the Post (agency) it belongs to, you can use a similar approach, but this time you'll use the Toolset Relationship API
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
Similarly you will get the ID's of the Agencies as I show above in a cred_save_data() hook https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
In that hook you will get the IDs of the Agencies the same way as I did in the example above.
Then, you will fire toolset_connect_posts() within that hook, to connect the new message to the parent Agencies.
You can do that as well with a foreach, that for each of the Agency ID's will fire something like:
toolset_connect_posts( 'book-author', $post_id, $your_url_param_id );
Please let me know if you need more help with this, I can however help only limitedly with custom code, as explained here https://toolset.com/toolset-support-policy/
Thank you!