refactor: replace sending transport from mail to sendmail

This commit is contained in:
Julien Oculi 2024-03-29 10:07:04 +01:00
parent 5e4d833378
commit 2a49a679c6

View file

@ -1,50 +0,0 @@
import { render } from 'jsx-email'
import { Mail } from '../types.ts'
export async function send(mail: Mail) {
const args: string[][] = [
['-s', mail.subject],
['-a', `From: ${mail.options.from}`],
['-a', `To: ${mail.to.map((contact) => contact.toString()).join(', ')}`],
['-a', 'MIME-Version: 1.0'],
['-a', `Content-Type: ${mail.options.contentType}`],
]
if (mail.options.cc.length) {
args.push([
'-a',
`CC:${mail.options.cc.map((cc) => cc.toString()).join(', ')}`,
])
}
if (mail.options.cci.length) {
args.push([
'-a',
`BCC:${mail.options.cci.map((cci) => cci.toString()).join(', ')}`,
])
}
if (mail.options.attachments.length) {
mail.options.attachments.forEach((attachment) =>
args.push([`-A=${attachment}`])
)
}
args.push([mail.to.map((account) => account.address).join(', ')])
const cmd = new Deno.Command('/usr/bin/mail', {
args: args.flat(),
stdin: 'piped',
stderr: 'inherit',
stdout: 'inherit',
})
const process = cmd.spawn()
const _body = typeof mail.body === 'string'
? mail.body
: await render(mail.body)
const writer = process.stdin.getWriter()
await writer.write(new TextEncoder().encode(mail.body.toString()))
await writer.close()
await process.output()
}