I need to dynamically send form notifications to multiple recipients. I have a list of user ID's already saved to the user's profile
I would like to have a form (similar to messages in the classified setup) that sends to all of the users in that list.
My idea was to use "Send notification to a WordPress user with an ID coming from a generic field in the form:" but it appears you can only use 1 there. Is there a way I could do this with a CSV string of IDs
I tried a suggestion from other posts but this function wouldn't send to a CSV list of IDs or even with one email address
example:
function customise_cred_notifications( $headers, $formid, $postid, $notification_name, $notification_number ) {
$myheaders = array();
if ( $formid == 9903 && $notification_name == 'Quotes2019' ) {
$recipients = 'user1@email.com,user2@email.com,user3@email.com';
$myheaders = array( 'To: ' . $recipients );
}
return array_merge($headers, $myheaders);
}
add_filter('cred_mail_header', 'customise_cred_notifications', 101, 5);
The code suggested there is:
function modify_recipients($recipients, $notification, $form_id, $post_id) {
if ( 1111 == $form_id) {
$countrysin = get_post_meta($post_id, 'wpcf-location-country', true);
global $wpdb;
$result = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'wpcf-email' AND post_id IN (SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'wpcf-country' AND meta_value = '$countrysin')");
//Add a BCC to log@emailaddress.com
foreach ($result as $row):
$email = $row->meta_value;
$recipients[] = array(
'to' => 'to',
'address' => $email,
'name' => '',
'lastname' => ''
);
endforeach;
}
return $recipients;
}
add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
Of course, that code won't work as it's customized to the site the user there created.
To adapt this to your site, you would have to start here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients
The recipients are an array of notification recipients. Each recipient is stored as an array with a to field (with possible values of 'to', 'cc', or 'bcc'), an address field (the email address), an optional name and lastname fields. This argument must be returned by the filter.
Since you use a generic Field to get the emails, and generic fields do not persist to the database, we cannot retrieve them now, as it's too late for the form's $_POST.
We'd need to get those addresses from a saved value.
I understand you saved the ID's of the Users where the emails should be sent to in a user field already.
So is this is a user edit form, where the user edited is determining what users get notified?
This would help to determine further how to do this.
I assume your generic field is made like this example, right?
https://toolset.com/documentation/user-guides/automated-email-notifications-with-cred/#send-email-notification-to-a-user-specified-in-a-generic-field
Very thorough answer! Thank you!. This will likely take me a few days to figure out but I appreciate all of that info it looks very promising.
You are correct that I have a user edit form with only a submit button shown to add/remove user ID's to the list. This is supplemented with a few shortcodes. The generic field is similar to your example.
Thank you Beda, I appreciate your response and am sure this will help someone out down the road as well
This looks promising, just having a glitch.
I tried a primitive test, excluding my fields which I can introduce later and hard-coded an array to see how it would do.
Thankfully, it is sending to multiple recipients, but only as "TO"
CC and BCC seem to have no effect. I can't have the recipients see who the other recipients are for this scenario.
It doesn't seem to make a difference if I change the drop-down in the form notification to BCC, or do it dynamically, it seems like the only emails that get sent are "to" emails. CC & BCC don't respond to any combination. Ideally I want them all BCC and will place one "to" field inside the form notification section.
The form processes the function and the first 2 recipients in this example. I change the others to "to" and all emails were received so I know it's getting all of the recipients:
any ideas why? I am not using any third party smtp plugins, just default wordpress.
example:
function modify_recipients($recipients, $notification, $form_id, $post_id) {
if ( 9903 == $form_id) {
$recipients[0] = array(
'to' => 'to',
'address' => 'me@email1.com',
'name' => '',
'lastname' => ''
);
$recipients[1] = array(
'to' => 'to',
'address' => 'me@email2.com',
'name' => '',
'lastname' => ''
);
$recipients[2] = array(
'to' => 'cc',
'address' => 'me@email3.com',
'name' => '',
'lastname' => ''
);
$recipients[3] = array( 'to' => 'bcc', 'address' => 'me@email4.com', 'name' => '', 'lastname' => '' );
$recipients[4] = array( 'to' => 'bcc', 'address' => 'me@email5.com', 'name' => '', 'lastname' => '' );
$recipients[5] = array( 'to' => 'bcc', 'address' => 'me@email6.com', 'name' => '', 'lastname' => '' );
}
return $recipients;
}
add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
It doesn't seem to make a difference if I change the drop-down in the form notification to BCC, or do it dynamically, it seems like the only emails that get sent are "to" emails. CC & BCC don't respond to any combination. Ideally, I want them all BCC and will place one "to" field inside the form notification section.
I actually think, BCC and CC completely break the email notifications.
As soon I chose that, on a user form, I received no more notifications. I do receive them when I set "To".
This seems a BUG, I reported it so we can fix it.
For now - logically seen - this will also break in the code.
I'll dig later if I can find a workaround to it.
I'm surprised you are receiving emails at all, but I did not yet try personally with the filter, which could be working partially because firing at a slightly different moment than the backend setting.
It is good to know I am not the only one seeing this and quite surprised it hasn't been spotted sooner.
Thank you for submitting the bug, hopefully it doesn't take too long to fix, I know you guys are quite busy already
There were some scenarios where I was not getting any notifications but with the current setup they were delivery to anything set as "TO" in the array
I appreciate you looking into a workaround as this functionality is needed right away, I am open to any workarounds even if temporary.
The only thought I've had so far is probably not a good way to do it, but I will mention it in case it gives you any ideas.
Instead of going the multiple recipients' route, try with multiple posts, each with their own single "TO" notification
I suspect this would be harder on the database having 10 identical posts instead of 1 post with 10 recipients. Secondly, I imagine creating 9 duplicate posts with different recipients would be much harder to do. I have thumbed through the documentation and support forums for any ideas like this and it does not sound ideal, but if you see any potential in going that route, please advise
Additionally, a long shot, as I don't understand how it all works, but if a different SMTP plugin were used, would it help facilitate using CC and BCC? or is the problem prior to that stage where it is processing header info?
or if there were a file within Toolset I could modify, I could update that file after each release. Again, not an ideal solution but I am open to just about anything right now
Yes, I was thinking about the second solution as well, however, I am not sure of any plugin that allows you such dynamic source and output choice like Toolset Forms does.
If you know of some that allow sending emails to a dynamic source of recipients (and has an easy way to catch those recipients) then yes, this could work.
Otherwise, I fear this needs to wait until it's fixed, however, to send emails with Forms to many users (populated dynamically) you can still update them to with all the recipients, that should work (in the code)
Instead of using BCC and CC just use To.
This has drawbacks but the emails are sent visibly in the To field (all of the additional emails are used.)
Hmm okay, I cannot do as you suggest. Recipients can not, in any way, see the other recipients, they are essentially competitors to each other and the email addresses need to remain confidential. The thought had crossed my mind to try something like ContactForms7, but that somewhat defeats the purpose of using toolset and is quite buggy itself.
I can adapt my side to have say 25 max recipients, then have 1 field in the post form for each recipient, ie) recipient1, recipient2, .... recipient25. If it were setup that way, do you think it would help the TO/CC/BCC issue or would we be able to loop each of the 25 recipients as a single TO message each instead of 1 message showing 25 contacts in the TO field?
I am perfectly okay with not using CC and BCC if we can reach all recipients with each of them seeing each other if that is more clear or helpful
Well, if you can add 25 single fields to the form with either populable or fixed emails, then you could create 25 separate (to) notifications each submitting to the one precise of 25 different email fields.
That is a lot but if it's required to keep those private probably the only (currently working) solution.
Okay that is a good idea, I can test that theory out tomorrow on 5-10 and see if its going to work. I will reply with how it works out.
I suppose doing it that way I could still use just the user ID as well.
Thanks for the suggestion, I will keep you posted
I have escalated this ticket for a resolution to the developers - let's use it for this single issue.
An erratum will also be published (I am waiting for it to be proofread).
I will keep you updated here about the particular issue, if you require other help meanwhile with other aspects, I suggest opening a new ticket so other supporters can dig in those requests, while we keep this here with the Developers.
Thank you. I tried as you suggested and it looks go so far! I've created 5 different notifications, each populating the TO field from a generic field with the user ID of the recipient.
I can work with this while the bugs get sorted out, very good temporary solution, thank you Beda!