Skip to content

Instantly share code, notes, and snippets.

@Lukas-Sachse
Created May 26, 2022 08:14
Show Gist options
  • Save Lukas-Sachse/0fc019385d14bb1859b1240617c9e601 to your computer and use it in GitHub Desktop.
Save Lukas-Sachse/0fc019385d14bb1859b1240617c9e601 to your computer and use it in GitHub Desktop.
Hook - Send email after form submission (text only)
/* Info: Created without using the extension-sdk */
/* Location: /extensions/hooks/YOUR_HOOK_NAME/index.js */
module.exports = function registerHook(
{ action },
{ services, exceptions, logger }
) {
const { MailService } = services;
const { ServiceUnavailableException } = exceptions;
action("anmeldungen.items.create", async (input, { schema }) => {
logger.info("ACTION ANMELDUNG CREATED");
logger.info(" - payload: " + JSON.stringify(input.payload)); // payload contains all used input fields from the form
logger.info(" - key: " + JSON.stringify(input.key)); // key contains the id of the created item
/* Creating new instances of the MailService */
const mailService = new MailService({ schema });
/* Sending the email. */
try {
await mailService.send({
to: input.payload.kontakt_email,
subject: "Anmeldung angekommen",
text: `Hallo,
Die Anmeldung von ${input.payload.vorname} ${input.payload.nachname}
als ${input.payload.rolle} ist erfolgreich bei uns angekommen.
Wir werden sie in den nächsten Tagen bearbeiten.`,
attachments: [
{
filename: "Anmeldung_angekommen.pdf",
path: `https://YOUR_DIRECTUS_URL/assets/YOUR_FILE_ID`, // could be any file url from the internet
},
],
});
} catch (error) {
throw new ServiceUnavailableException(error);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment