Add contact list to email notifications
-
Hi there,
I would be very happy if you can help me.
I would like to specify a contact list as recipient in my Forminator form in the email notifications (over 200 contacts).
But currently you can only specify a limited number of recipients and you always have to specify them manually. Is there any way to enter a contact list from another plugin as a shortcode in the recipient list?
Or is there any other way to solve this problem, maybe with a php snippet?
Many thanks in advance and kind regards.
-
I hope you are doing good today.
I pinged our Forminator and SLS Team to review your query. We will post an update here as soon as more information is available.
Kind Regards,
KrisHello @thankssforhelp ,
Here’s an example snippet(mu-plugin) to get multiple e-mails:
add_shortcode('multiple_mails', 'wpmudev_generate_multiple_mails'); function wpmudev_generate_multiple_mails() { $mails = array('[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]'); return json_encode($mails); } add_filter( 'forminator_form_get_admin_email_recipients', 'wpmudev_set_multiple_mail_recipients', 10, 5 ); function wpmudev_set_multiple_mail_recipients( $email, $notification, $prepared_data, $module, $entry ) { if( $prepared_data['form_id'] != 361 ){ return $email; } if( $notification['recipients'] == '{multiple-mails}' ) { $mails = json_decode( do_shortcode('[multiple_mails]') ); //$mails = array('[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]'); if( $mails ){ $email = $mails; } } return $email; }
This is an example shortcode that returns the emails and then I use it in the filter here
$mails = json_decode( do_shortcode('[multiple_mails]') );
If you want to directly use the array then the above line can be commented and the following line can be un-commented:
//$mails = array('[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]');
You need to change the form ID from 361 to your form’s ID and use this {multiple-mails} macro in the recipient field.
Please add a must-use plugin to your site’s wp-content/mu-plugins folder like this https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins with the above code.
kind regards,
KasiaThank you very much! Does it make a difference if I use the filter or the array right away? Does this have a specific function?
Do I have to extend or change the email list in the .php file every time my recipient list changes? Or is there a way that the list is automatically fetched from my e.g. Google contact list? – I.e. if I have new contacts on Google, the new email is automatically added and if I delete a contact also the email is deleted. This would be great, since I will have about 200 which might continously change.
Thank you very much!
Thank you very much! Does it make a difference if I use the filter or the array right away? Does this have a specific function?
It doesn’t really make any difference “technically”. You can use just this part of code safely
add_filter( 'forminator_form_get_admin_email_recipients', 'wpmudev_set_multiple_mail_recipients', 10, 5 ); function wpmudev_set_multiple_mail_recipients( $email, $notification, $prepared_data, $module, $entry ) { if( $prepared_data['form_id'] != 361 ){ return $email; } if( $notification['recipients'] == '{multiple-mails}' ) { $mails = array('[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]'); if( $mails ){ $email = $mails; } } return $email; }
Just set the form ID and emails in the code and put the {multiple-mails} macro in recipients field of notification.
Do I have to extend or change the email list in the .php file every time my recipient list changes?
Yes, regardless of whether you’d use the “shortcode” or “array” version of the code. The only difference would be only in which line of the code you need to set your e-mail addresses.
Or is there a way that the list is automatically fetched from my e.g. Google contact list? – I.e. if I have new contacts on Google, the new email is automatically added and if I delete a contact also the email is deleted. This would be great, since I will have about 200 which might continously change.
I understand how useful this would be but it’d require way more complex code, way beyond the scope of this support, I’m afraid.
It would be possible, I believe, to extend this code relatively easily to read the list of e-mail addresses from a text file or from some designated record of of site’s database (e.g. from selected record in “wp_options” table, preferably) so if you’re interested in one of these ways – let us know.
But if you need it to read and handle addresses from Google contacts, any other external service or some 3rd party plugin, you may need to consider hiring a developers who’d code it for you.
Kind regards,
AdamHi Adam @wpmudev-support8 ,
first of all thank you very much for the detailed feedback
It would be possible, I believe, to extend this code relatively easily to read the list of e-mail addresses from a text file or from some designated record of of site’s database (e.g. from selected record in “wp_options” table, preferably) so if you’re interested in one of these ways – let us know.
I would indeed be very grateful if the code can be extended in the way described.
I’m using the WP plugin TablePress, which includes all relevant email addresses of the recipients and updates automatically all the time.
Would it be possible with the code to fetch these email addresses here? The mentioned plugin also offers a shortcode for the table.
Thank you very much in advance.
Kind regards
Hello @thankssforhelp !
Hope you’re having a good day!
Extending to read from a database record if the emails are stored there in an array is very simple:
$mails = array('[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]');
Should be changed to:
$mails = get_option('your_specified_option_name');
And then your_specified_option_name contains the emails.
Reading from a file is also easy to achieve:
$mails = array_map('str_getcsv', file('/path/to/data.csv'));
This will then contain the email array if the emails are stored 1 per line.
The difficult part mentioned by Adam earlier would be to extract the data from a 3rd party plugin – this is the part which is unfortunately out of scope of our support.
Kind regards,
PawelHi Pawel @wpmudev-support9 ,
first of all thank you for the answer!
I tried lots of times as described, but unfortunately nothing happened. For testing purposes I also tried
$mails = get_option('admin_email')
; without success.So the whole code for testing looked like this:
<?php add_filter( 'forminator_form_get_admin_email_recipients', 'wpmudev_set_multiple_mail_recipients', 10, 5 ); function wpmudev_set_multiple_mail_recipients( $email, $notification, $prepared_data, $module, $entry ) { if( $prepared_data['form_id'] != 8652 ){ return $email; } if( $notification['recipients'] == '{multiple-mails}' ) { $mails = get_option('admin_email'); if( $mails ){ $email = $mails; } } return $email; }
Unfortunately, I didn’t get anywhere like that.
Possibly I am making a mistake somewhere.
I found out in the meantime that TablePress – this is the plugin I’m working with – stores all data as JSON-encoded two-dimensional arrays in wp_posts (in my database “wp_posts” is somehow called jhc_posts).
So I could find the following entry under phpMyAdmin > jhc_posts:
https://drive.google.com/file/d/1gKvsfFxN8u1D1jy3H0kVGgtUQEVAJMFP/view?usp=sharing
You can see that all relevant email addresses are stored here. How can I now transfer these into my code? With
$mails = get_post(8651)
I already tried, but failed.I would be very grateful if you can help me here. Thanks in advance for the valuable support!
Kind regards
If that data is store in post, as post content, then you are on a right track but we still need to access post content rather than entire post object and we’d need to parse that data (as post_content will contain just regular string).
This should fetch it, convert to array and remove empty values, assuming that the “format” of that data is always the same as on your screenshot:
$my_post = get_post(8651); $mails = str_replace( '"', '', str_replace( ']', '', str_replace( '[' , '', $my_post->post_content))); $mails = array_filter( explode( ",", $mails ), function($value) { return ( $value !== NULL && $value !== FALSE && $value !=='' ); });
Best regards,
AdamHi Adam @wpmudev-support8 ,
so many thanks! it now works wonderfully and smoothly, so now a big problem could be solved. You are the best support! ??
One last question regarding this: are there any limits on sending notification emails or the number of recipients? Would it work, for example, if 50 notification mails go out to 200 recipients per day?
Thanks a lot for your support and kind regards
I’m glad I could help!
No limits there “on plugin end” as far as I’m aware so whatever server/site can handle (which would be related to resources but also to server/SMTP – depends on which one you are using to send mail – limits) should work.
But I can’t really “estimate” here and also I wouldn’t really use this type of implementation to send “mass emails”. There are potential consequences such as e.g. being recognized as spam source or abusing server resources and similar. For a “mass mailing” it’s way better to use a dedicated 3rd-party services such as e.g. Mailgun or similar.
Best regards,
AdamHi Adam,
perfect, thanks a lot! – I’ll try it out.
Kind regards
- The topic ‘Add contact list to email notifications’ is closed to new replies.