mailer/cli/send.ts
2024-03-28 22:32:46 +01:00

68 lines
1.9 KiB
TypeScript

import { Command } from '@cliffy/command/mod.ts'
import { format } from '../src/format.ts'
import { Contact } from '../src/contact.ts'
// import { send } from '../src/send.ts'
import { send } from '../src/transporter.ts'
export const cmd = new Command()
.name('send')
.description('Send a mail.')
.option(
'-f, --from <account:string>',
'From mail account or short name from config.',
{ default: '!me' },
)
.option('--cc <recipient:string>', 'Copy carbon.', { collect: true })
.option('--cci <recipient:string>', 'Copy carbon invisible.', {
collect: true,
})
.option('-a, --attachments <file:file>', 'Attachments.', { collect: true })
.option('--content-type <type:string>', 'Mail body Content-Type.', {
default: 'text/html; charset=utf-8',
})
//.option('-t, --template <name:template>', 'HTML template from config', { default: 'basic' })
.arguments('<to:string> <subject:string> <body:string>')
.action(
async ({ from, cc, cci, attachments, contentType }, to, subject, body) => {
const fromContact: Contact = await (async () => {
if (from === '!me') {
const whoami = new Deno.Command('whoami', {
stderr: 'inherit',
})
const { stdout } = await whoami.output()
const rawName = new TextDecoder().decode(stdout).trim()
const name = encodeURIComponent(rawName)
return {
name: `${name} de Cohabit`,
address: `_${name}_@cohabit.fr`,
}
}
if (from.startsWith('!')) {
//@ts-ignore try expand
return expand(from.slice(1))
}
return Contact.fromString(from)
})()
const toContact = Contact.fromString(to)
const mail = format(fromContact, toContact, subject, body, {
cc: cc?.map(Contact.fromString) ?? [],
cci: cci?.map(Contact.fromString) ?? [],
attachments: attachments ?? [],
contentType,
})
await send(mail)
},
)
if (import.meta.main) {
if (Deno.args.length) {
cmd.parse(Deno.args)
} else {
cmd.showHelp()
}
}