mailer/cli/send.ts

59 lines
1.6 KiB
TypeScript

import { Command } from '@cliffy/command/mod.ts'
import { Contact } from '../src/contact.ts'
import { send } from '../src/send.ts'
import { Mail } from '../types.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('-t, --template <name:template>', 'HTML template from config', { default: 'message' })
.arguments('<to:string> <subject:string> <body:string>')
.action(
async ({ from, cc, cci, attachments }, 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 mail: Mail = {
from: fromContact,
to: [Contact.fromString(to)],
subject,
body,
options: {
cc: cc?.map(Contact.fromString) ?? [],
cci: cci?.map(Contact.fromString) ?? [],
attachments: attachments ?? [],
},
}
await send(mail)
},
)