Skip to content

Sending and templating emails

Benson Cho edited this page Jul 26, 2024 · 1 revision

Templating with pug

For modularity we use pug as our templating library. All templates should be compiled and rendered in the MailService only, as we have a preset TEMPLATE_BASE_PATH that allows us to get files from server\src\business-layer\templates (all templates must be created here).

For example usage of compiling .pug templates:

Consider the pug template BookingConfirmation.pug

// Get the function that allows us to inject variables
const compiledFunction = compileFile(`${TEMPLATE_BASE_PATH}/BookingConfirmation.pug`)

// Use the compiled html as an email body
sendFunction(compiledFunction({name:"the name"}))

NodeMailer setup

We use nodemailer with gmail as of now for simplicity - refer to work in https://github.com/UoaWDCC/uasc-web/pull/513

Required env:

      /**
       * The email from which NodeMailer emails should be sent from
       *
       * You will need to have access to this account to obtain the password
       */
      NODE_MAILER_EMAIL: string
      /**
       * The **App Password** for the gmail account specified by `NODE_MAILER_EMAIL`.
       *
       * Refer to https://knowledge.workspace.google.com/kb/how-to-create-app-passwords-000009237
       */
      NODE_MAILER_PASSWORD: string