Created
May 26, 2022 07:50
-
-
Save Lukas-Sachse/c1201dc795c01daba47cdcf7982e270f to your computer and use it in GitHub Desktop.
Hook - Send email after form submission (editable templates)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Info: Created without using the extension-sdk */ | |
/* Info: For using editable templates i have to mimic the way directus using email templates. */ | |
/* Location: /extensions/hooks/YOUR_HOOK_NAME/index.js */ | |
module.exports = function registerHook( | |
{ action }, | |
{ services, exceptions, logger } | |
) { | |
const { ItemsService, MailService, FilesService } = services; | |
const { ServiceUnavailableException } = exceptions; | |
const { Liquid } = require("liquidjs"); | |
const path = require("path"); | |
action("anmeldungen.items.create", async (input, { schema }) => { // 'anmeldungen' is my form collection | |
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, ItemsService, FilesService. */ | |
const mailService = new MailService({ schema }); | |
const emailVorlagenItemService = new ItemsService("email_vorlagen", { | |
schema, | |
}); | |
const filesService = new FilesService({ schema }); | |
/* Create new instance of the LiquidEngine with path to the templates folder. */ | |
const liquidEngine = new Liquid({ | |
root: [ | |
path.resolve(process.env.EXTENSIONS_PATH, "templates"), | |
path.resolve(__dirname, "templates"), | |
], | |
extname: ".liquid", | |
}); | |
/* Reading the template from the database. */ | |
const templateArray = await emailVorlagenItemService.readByQuery({ | |
filter: { name: { _eq: "anmeldung_angekommen" } }, | |
fields: ["*.*"], | |
}); | |
/* Rendering the template with the data from the input.payload. */ | |
const template = await liquidEngine.parseAndRender( | |
'{% layout "base" %} {% block content %}' + | |
templateArray[0].inhalt + | |
"{% endblock %}", | |
{ | |
vorname: input.payload.vorname, | |
nachname: input.payload.nachname, | |
rolle: input.payload.rolle, | |
} | |
); | |
/* Getting the file IDs from the attachements field inside my template item. */ | |
const directusFilesIds = templateArray[0].anhaenge.map((anhang) => { | |
return anhang.directus_files_id; | |
}); | |
/* Getting the file items from the database. */ | |
const files = await filesService.readByQuery({ | |
filter: { id: { _in: directusFilesIds } }, | |
fields: ["id", "filename_download", "type"], | |
}); | |
/* Creating an array of objects with the filename, path and contentType of the files. */ | |
const attachments = files.map((file) => { | |
return { | |
filename: file.filename_download, | |
path: `https://YOUR_DIRECTUS_URL/assets/${file.id}`, | |
contentType: file.type, | |
}; | |
}); | |
/* Sending the email. */ | |
try { | |
await mailService.send({ | |
to: input.payload.kontakt_email, | |
subject: templateArray[0].betreff, | |
html: template, | |
attachments: attachments, | |
}); | |
} catch (error) { | |
throw new ServiceUnavailableException(error); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment