mailer/cli/send.ts

73 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

2024-07-15 12:10:57 +02:00
import { Command } from '@cliffy/command'
2024-03-28 22:32:46 +01:00
import { Contact } from '../src/contact.ts'
import { send } from '../src/send.ts'
import type { Mail } from '../types.ts'
import { templates, templateType } from './_templates_loader.ts'
import { promptProps } from './_prompt_template.ts'
2024-04-03 12:49:00 +02:00
//TODO completions for "--from"
//TODO require sudo for "--from !== !me"
2024-03-28 22:32:46 +01:00
export const cmd = new Command()
.name('send')
.description('Send a mail.')
.type('template', templateType)
2024-03-28 22:32:46 +01:00
.option(
'-f, --from <account:string>',
'From mail account or short name from config.',
{ default: '!me' },
)
.option('-r, --recipient <recipient:string>', 'Recipient (to) of the mail.', {
required: true,
collect: true,
})
2024-03-28 22:32:46 +01:00
.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('<subject:string>')
2024-03-28 22:32:46 +01:00
.action(
async ({ from, recipient, cc, cci, attachments, template }, subject) => {
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 new Contact({
2024-03-28 22:32:46 +01:00
name: `${name} de Cohabit`,
address: `_${name}_@cohabit.fr`,
})
2024-03-28 22:32:46 +01:00
}
if (from.startsWith('!')) {
return Contact.expand(from.slice(1))
2024-03-28 22:32:46 +01:00
}
return Contact.fromString(from)
})()
const selectedTemplate = templates.get(template)!
const props = await promptProps(selectedTemplate)
const mail: Mail = {
from: fromContact,
to: recipient.map((to) => Contact.fromString(to)),
subject,
body: selectedTemplate.builder(props)!,
options: {
cc: cc?.map(Contact.fromString) ?? [],
cci: cci?.map(Contact.fromString) ?? [],
attachments: attachments ?? [],
},
}
2024-03-28 22:32:46 +01:00
await send(mail)
},
)