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) { export async function send(mail: Mail) {
const { html, text } = await renderTemplate(mail.body) const { html, text } = await renderTemplate(mail.body)
return transporter.sendMail({ return (await transporter()).sendMail({
from: mail.from.toString(), from: mail.from.toString(),
to: mail.to.map((contact) => contact.toString()), to: mail.to.map((contact) => contact.toString()),
cc: mail.options.cc.map((cc) => cc.toString()), cc: mail.options.cc.map((cc) => cc.toString()),

View file

@ -1,20 +1,23 @@
// @deno-types="npm:@types/nodemailer" // @deno-types="npm:@types/nodemailer"
import nodemailer from 'nodemailer' import nodemailer from 'nodemailer'
const dkimPath = '/home/julien/dkim_sendmail_keys/dkim_sendmail_cohabit_fr.pem' export async function transporter() {
const dkimPrivateKey = await Deno.readTextFile(dkimPath).catch((cause) => { const dkimPath =
throw new Error(`unable to load DKIM private key from "${dkimPath}"`, { '/home/julien/dkim_sendmail_keys/dkim_sendmail_cohabit_fr.pem'
cause, 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({ return nodemailer.createTransport({
sendmail: true, sendmail: true,
newline: 'unix', newline: 'unix',
path: '/usr/sbin/sendmail', path: '/usr/sbin/sendmail',
dkim: { dkim: {
domainName: 'cohabit.fr', domainName: 'cohabit.fr',
keySelector: 'sendmailY2024M03', keySelector: 'sendmailY2024M03',
privateKey: dkimPrivateKey, privateKey: dkimPrivateKey,
}, },
}) })
}