- Blog/
Send email from the command line with Nushell and Himalaya
I will use:
- Nushell Custom Completions
- Nushell Custom Commands
- Nushell Modules
- Nushell builtins
Yeah, it kinda looks like I will use Nushell 🐘 a lot.
But the heavy email lifting will be done by the excellent Himalaya pimalaya/himalaya: CLI to manage emails.
The parsing of my vcard contact files is done with the Nushell formats plugin: nu_plugin_formats - crates.io: Rust Package Registry
Let’s get started:
I create a file called numail.nu and put it in my modules
folder.
In this file I start by defining a function, or Custom Completion, that will return a list of all contacts with an email address.
def recipient [] {
{
options: {
case_sensitive: false,
completion_algorithm: fuzzy,
},
completions: (glob /home/lk/.contacts/Google/default/*.vcf # these are vcard files for contacts
| open ...$in | get properties | each { transpose -r } # requieres nu_plugin_formats
| flatten | where EMAIL? != null and FN? != null
| select FN EMAIL
| insert recipient { format pattern r##'{FN} <{EMAIL}>'## }
| get recipient )
}
}
You can read more about Custom Completions here: Custom Completions | Nushell
I can now select from this list of contacts in a Custom Command like so:
export def numl [
...recipient: string@recipient
--attachment (-a): string
--from (-f): string = "[email protected]"
--greeting (-f): string = "Kære "
--signature (-s): string = "\n\nMed venlig hilsen,\nLennart"
--subject (-s): string = "Vigtigt"
] {
let message = $in
let recipient = $recipient | str join " " | parse "{name} <{to}>"
let eml = $"From: ($from)\nTo: ($recipient.to | to text --no-newline)\nSubject: ($subject)(char nl)(char nl)($greeting)($recipient.name | to text --no-newline)(char nl)(char nl)($message)($signature)( if ($attachment | is-empty) {} else {$'(char nl)(char nl)<#part filename=($attachment)><#/part>'} )"
$eml | ^himalaya template send $in # requieres CLI to manage emails https://github.com/pimalaya/himalaya
}
I place the above Custom Command, together with the Custom Completion from, earlier in a Nushell Module - just a file called numail.nu.
I then load the module like so:
use numail.nu *
Now in my Nushell REPL I have the numl
Custom Command at my disposal. And because of Custom Completions I can simply write:
numl
and then the first few characters in a recipient’s name and then the tab key and Nushell will provide me with a simple menu to choose from among my contacts with names containing the few characters I wrote.
The email message itself is “piped” in like so.
"Det er bare en test" | numl Lennart <[email protected]>
Message successfully sent!
Bonus:
If you would prefer to write the message of the email in your editor instead of directly on the command line, use the following:
vipe --suffix eml | numl Lennart <[email protected]>
This requires that you have the moreutils
package installed.
Now that I can send mails directly from the command line, sending mails becomes scriptable. And when something is scriptable it also becomes easier to automate and to integrate with artificial intelligence.
But that is a subject for another article.