Here is my code for a custom hook that i use to expand directus 9.
I want to send a mail when a specific field in the events table gets updated. For now iÄm only checking if the events table gets updated. Then through the MailService i want to send an email. the code executes and logs 'mail sent' but there is no mail going out.
I have a custom template also which follows after the code. And indeed the template file is picked up and recognized. I check by removing it, then i get an error that the template is missing.
/extensions/hooks/event-time-update/index.js
module.exports = function registerHook({ action }, { services, exceptions }) {
const { MailService } = services;
const { ServiceUnavailableException, ForbiddenException } = exceptions;
// Send mail to admin when event time is updated
action('items.update', async ({ collection }, { schema }) => {
if (collection !== 'events') return;
const mailService = new MailService({ schema });
try {
await mailService.send({
to: 'abc@gmail.com',
subject: 'Event time updated',
text: 'Event time updated',
template: {
name: 'event-time-update',
data: {
collection: collection,
},
},
});
console.log('mail sent');
} catch (error) {
console.error(error);
throw new ServiceUnavailableException(error);
}
return;
});
};
/extensions/templates/event-time-update.liquid
{% layout "base" %}
{% block content %}
<p>
Die Veranstalungszeit der Veranstaltung <i>{{ projectName }}</i> wurde geändert.
</p>
<p style="text-align: center; padding: 20px 0;">
<a href="{{ url }}">
<b>{{ projectName }} ansehen</b>
</a>
</p>
<p>
Thank you,<br>
The {{ projectName }} Team
</p>
{% endblock %}