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

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.

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);

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));
}
});
1 Like