One-line CLI mail to multiple recipients with inline content

Table of Contents

Intro

I found a one-liner that allowed me to test the outgoing mail setup on a remote server. The following one-liner worked, and introduced me to some new-to-me functionality with Bash and Mail.

mail -s "Testing Not registration confirmation" -r from@address.com to1@example.com,to2@example.com <<< 'testing 4'

Analysis

mail

The command assumes that you already have a working Mail1 aka PostFix setup on the server you are running on, which means a sendmail setup. I’m not getting in to that here but am grateful for a system admin who takes care of that stuff for us.

  1. mail subject -s

  2. mail from: -r

  3. mail to addresses

    Notice that they are unquoted, comma seperated, with no space following the comma.

Shell “here string” <<<

Provides input2 to the prompt that would result from the mail command, so that you can run it without a prompt showing up.

The <<< operator3 is called the “here string” and may have originated with Korn shell4 before being brought to Bash5.

Footnotes

1 Mail setup is explained for Ubuntu users here https://www.linuxfordevices.com/tutorials/linux/mail-command-in-linux . There is a good round-up of similar commands for other systems here https://linuxhint.com/umail-command-linux/ .

2 I found other, more common input operators here https://tecadmin.net/pass-command-line-arguments-in-shell-script/

3 The “Here String” is not listed on many bash operator listings, though I found an otherwise useful listing here https://linuxhint.com/bash_operator_examples/ as well as https://www.thegeekdiary.com/6-bash-shell-command-line-chaining-operators-in-linux/z

4 Korn documentation and history on this operaton at http://nnc3.com/mags/unix3/korn/ch07_01.htm#korn2-CHP-7-SECT-1.2

5 None of the history, but some decent examples at https://www.landoflinux.com/linux_bash_scripting_here_strings.html

Tory Anderson avatar
Tory Anderson
Web App Engineer, Digital Humanist, Researcher, Computer Psychologist