This is what I tried:
$ mailx -s "test email" cloud
Cc:
"again and agina"
.
EOT
Or,
$ mailx -s "test email" cloud
Cc:
"this is the first email"
<< EOT
But after pressing Enter nothing happens. Why?
This is what I tried:
$ mailx -s "test email" cloud
Cc:
"again and agina"
.
EOT
Or,
$ mailx -s "test email" cloud
Cc:
"this is the first email"
<< EOT
But after pressing Enter nothing happens. Why?
There is nothing magical about the thee-letter-string EOT
. You have probably seen it used as a delimiter in here-document redirections in shell scripts from time to time. Almost any word may be used as a delimiter for a here-document redirection, although it's customary to use a short-ish descriptive word written in all upper-case letters; so you could, for example, send a message with mailx
by giving the utility the message on its standard input stream like so:
mailx -s 'test message' myself <<'END_MESSAGE'
This is the message.
Possibly on many lines.
END_MESSAGE
This would use mailx
non-interactively to send an email consisting of two lines of text to the user myself
. The body of the message is quoted, i.e., the shell won't try to expand variables etc., in it, due to the quoting of the initial delimiter ('END_MESSAGE'
).
However, from seeing the two commands in the question, you appear to want to use mailx
interactively to type a message into the utility.
If you have had the dot
option set in your ~/.mailrc
file (set dot
), then typing a single dot on a line by itself as you did in the first part of your question would end the message body and cause the email to be sent:
$ cat ~/.mailrc
set dot
$ mailx -s 'test message' myself
Cc:
This is the message.
Possibly on many lines.
.
Typing the lone dot and pressing Enter causes the message to be sent.
If you don't have the dot
option set or if you have the nodot
option set in ~/.mailrc
, the message body is instead ended using Ctrl+D on an otherwise empty line.
Pressing Ctrl+D sends (commits, submits) the current line to the program waiting for input, and if the current line is empty, this will signal the end of input. This is true not just for mailx
but for most programs reading interactive input from their standard input stream.
Using .
on an empty line is also how you signal the end of user input in the ed
editor when finishing entering text after issuing the i
, a
, or c
command to insert, append or change the text in the current editing buffer. It wouldn't surprise me if mailx
inherited this custom from ed
.
As my unprivileged user I typed:
$ mailx -s "test email" root <<EOTx
> "this is the first email"
> EOTx
$
and a little while later:
Return-Path: <XXXXXX@XXXXXX.home>
X-Original-To: root
Delivered-To: root@XXXXXX.home
Received: by XXXXXX.home (Postfix, from userid 1000)
id ADEF4C0CF994; Sat, 27 Aug 2022 20:32:56 +0100 (BST)
Date: Sat, 27 Aug 2022 20:32:56 +0100
To: root@XXXXXX.home
Subject: test email
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <20220827193256.ADEF4C0CF994@XXXXXX.home>
From: XXXXXX <XXXXXX@XXXXXX.home>
X-Evolution-Source: mbox:/var/spool/mail/XXXXXX
"this is the first email"
root's mail is directed to my unprivileged account.
Doneal's comment is accurate as far as it goes, however your final line of << EOT
is the error in that case. It should have been EOT
without the redirection.
EOT
are not special in any way other than that the "heredoc" is introduced and terminated with them. You could have more easily avoided the whole thing and used Ctrl/D to terminate mailx
reading from stdin (the keyboard)
– Chris Davies
Aug 27 '22 at 19:47
EOF
wouldn't have helped them, since they didn't have the here-doc redirection in the command (despite a comment telling them to do that)
– ilkkachu
Aug 27 '22 at 20:09
mailx
interactively in the question and that using a here-document would instead use the utility non-interactively (it would no longer prompt for the "carbon-copy" recipient list, for example, and variables etc. will be expanded by the shell if you don't quote the here-document). It may possibly be worth mentioning this. I don't know if it makes any difference whatsoever to the user.
– Kusalananda
Aug 27 '22 at 20:15
mailx -s "test email" cloud << EOT
, but the command they've added in the question is still mailx -s "test email" cloud
.
– ilkkachu
Aug 27 '22 at 20:29
<< EOT
to EOT
in the second example in the question wouldn't help, as one would also have to add the here-doc redirection on the command line; it was missing from the sample run in the question. You added it here of course, but that statement in the last paragraph of this answer appears to ignore the other part of problem the attempt in the question had.
– ilkkachu
Aug 28 '22 at 15:01
EOT
be recognized as the end of input? Trymailx -s "test email" cloud << EOT
to recognize the end of the heredoc. – doneal24 Aug 27 '22 at 17:29