How can I send email when a collection entry is created?

Collection
“Mail”
user id
customer id
email id

when an entry is saved to Mail by the user, want to send mail to a customer based on id of customer and email template. It will use “mailer” of config.yaml

1 Like

Create a file named bootstrap.php in in the config folder config/bootstrap.php with the following snippet:

<?php

$app->on('collections.save.after', function($name, $entry, $isUpdate) {

   if(!$isUpdate) {
      $this->mailer->mail('target@email.com', "New entry created in {$name}", json_encode($entry));
   }
});

This example ist just to get an idea.

1 Like

This works!!
Just one more thing, is it possible to filter other collection to be attached in the mail.

Thanks for the response.

you should be able to use something like this:

$res = cockpit('collections')->find('items', [
   'filter'=> [],
   'fields' => ['name'=>1, '_order'=>1]
];

$this->mailer->mail('target@email.com', "New entry created in {$name}", $res);
1 Like

This is what I was looking for, Thanks for the support.

<?php

$app->on('collections.save.after', function($name, $entry, $isUpdate) {

   if(!$isUpdate) {
$res = cockpit('collections')->find('collectionName', [
       'filter'=> ["_id"=>$entry["referenceKey"]],
       'limit'=> 1
    ]);
    $item = $res[0];

    $this->mailer->mail($item["fieldname"], "New entry created in {$name}", json_encode($item));
}
});
2 Likes

It doesn’t work in the second version?

UPD:
I figured out, the trigger changed to content.item.save

1 Like

Where shall I create the bootstrap file in the new version also what if I am using spaces How can I create a trigger for a specific space