mailer/cli/send.ts

59 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-03-28 22:32:46 +01:00
import { Command } from '@cliffy/command/mod.ts'
import { Contact } from '../src/contact.ts'
import { send } from '../src/send.ts'
import { Mail } from '../types.ts'
2024-03-28 22:32:46 +01:00
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' })
2024-03-28 22:32:46 +01:00
.arguments('<to:string> <subject:string> <body:string>')
.action(
async ({ from, cc, cci, attachments }, to, subject, body) => {
2024-03-28 22:32:46 +01:00
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 ?? [],
},
}
2024-03-28 22:32:46 +01:00
await send(mail)
},
)