feat(chore): lazy load dkim key

This commit is contained in:
Julien Oculi 2024-04-03 17:05:55 +02:00
parent b43c5e5451
commit 07be4fdb95
2 changed files with 19 additions and 16 deletions

View file

@ -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()),

View file

@ -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,
},
})
}