From 07be4fdb955972554884ccfc464f4905ff6fea38 Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Wed, 3 Apr 2024 17:05:55 +0200 Subject: [PATCH] feat(chore): lazy load dkim key --- src/send.ts | 2 +- src/transporter.ts | 33 ++++++++++++++++++--------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/send.ts b/src/send.ts index 84568aa..316215b 100644 --- a/src/send.ts +++ b/src/send.ts @@ -5,7 +5,7 @@ import { transporter } from './transporter.ts' export async function send(mail: Mail) { const { html, text } = await renderTemplate(mail.body) - return transporter.sendMail({ + return (await transporter()).sendMail({ from: mail.from.toString(), to: mail.to.map((contact) => contact.toString()), cc: mail.options.cc.map((cc) => cc.toString()), diff --git a/src/transporter.ts b/src/transporter.ts index 388b5c7..248194d 100644 --- a/src/transporter.ts +++ b/src/transporter.ts @@ -1,20 +1,23 @@ // @deno-types="npm:@types/nodemailer" import nodemailer from 'nodemailer' -const dkimPath = '/home/julien/dkim_sendmail_keys/dkim_sendmail_cohabit_fr.pem' -const dkimPrivateKey = await Deno.readTextFile(dkimPath).catch((cause) => { - throw new Error(`unable to load DKIM private key from "${dkimPath}"`, { - cause, +export async function transporter() { + const dkimPath = + '/home/julien/dkim_sendmail_keys/dkim_sendmail_cohabit_fr.pem' + const dkimPrivateKey = await Deno.readTextFile(dkimPath).catch((cause) => { + throw new Error(`unable to load DKIM private key from "${dkimPath}"`, { + cause, + }) }) -}) -export const transporter = nodemailer.createTransport({ - sendmail: true, - newline: 'unix', - path: '/usr/sbin/sendmail', - dkim: { - domainName: 'cohabit.fr', - keySelector: 'sendmailY2024M03', - privateKey: dkimPrivateKey, - }, -}) + return nodemailer.createTransport({ + sendmail: true, + newline: 'unix', + path: '/usr/sbin/sendmail', + dkim: { + domainName: 'cohabit.fr', + keySelector: 'sendmailY2024M03', + privateKey: dkimPrivateKey, + }, + }) +}