Hi,
I misuse the comments plugin for a small pseudo forum. Is it possible to send out email notifications to a set of users when a new comment has been submitted?
Hi,
I misuse the comments plugin for a small pseudo forum. Is it possible to send out email notifications to a set of users when a new comment has been submitted?
Which plugin?
Oh, I’m sorry, it is the “commentions” plugin.
You can use the commentions.add:after
hook to send emails after a new comment is submitted. There is an example in the documentation that you could extend to your needs.
The assumed use case in the example is a notification for the admin, i.e. the hook is triggered on submission of a comment; in case of a moderated workflow this would be the moment it is enqueued for moderation, not published. In order to trigger something when enqueued comments get published, use the commentions.update:after
hook instead - the example in its documentation actually covers exactly the use case of doing something after a post is changed from “pending” to “approved”.
Thank you!
It took me a few days to try it, hence this late reply, but it worked instantly. Now I just need to configure an email template, I guess.
The emails contain an array with all the data of the fields. But how can I convert that into normal text? I tried
template => notification
but that didn’t work.
Could you please provide more context?
The body of the email, that notifies of a new comment contains an array of the stuff that is in the fields/ Text files. Its this:
Array
(
[authenticated] => 1
[name] => Jörn
[status] => approved
[text] => test test
[timestamp] => 2020-11-03 19:55
[type] => comment
[uid] => tgxd64yvlq
)
Now I would like to somehow “convert” that into something more regular.
My Kirby email setup looks like this:
'email' => [
'transport' => [
'type' => 'smtp',
'host' => 'server-theserver-nserver.de',
'port' => 587,
'security' => true
]
],
The hook form the plugin is like:
'hooks' => [
'commentions.add:after' => function ($page, $data) {
try {
kirby()->email([
'from' => 'admin@muspaeds.de',
'to' => [
'admin@muspaeds.de',
'someone@muspaeds.de',
'anotherone@muspaeds.de
],
'subject' => 'Neuer Beitrag auf ' . $page->title(),
'body'=> print_r($data, true),
]);
} catch (Exception $error) {
echo $error;
}
}
],
I wanted to define a email template in my templates/ emails folder, but if I add:
template => 'my-template'
it doesn’t do anything, mostly because I don’t know what to do.
The code above has a missing quote after 'anotherone@muspaeds.de
, is that just a copy past error?
If you head over to the documentation: https://getkirby.com/docs/guide/emails#html-plain-text
you see that you set a template with the template option (note that the key must have single quotes as well, so 'template' => 'template-name'
).
You can also pass a data array to the template (instead of your body), then each variable you define in the data array is available in your template as in the example.
So
'data' => [
'name' => 'Peter',
'text' => 'welcome to our wonderful email list'
]
can then be used as echo $name
in the template.
yes, it was copy/ paste errors or just Schlamperei meinerseits.
The data array is from the commentions plugin, I can’t define it, can’t I?
But you can pass it to the data variable:
'data' => $data,
Oh. Ok. Yes!
Thank you very much!!