47

I am working on a KornShell (ksh) script running on a Solaris server that will send out an email when and error condition is met. I am sending the email via mailx.

Question: How do I set the "From" email address on the mailx command?

Current Code:

echo ${msg_txt} | mailx -s "Script Failure" ${to_email}

Note: The command works fine, however, the "From" is the name of the user I am running the script as and I would like for this to another email address.

How would I accomplish this?

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
AieshaDot
  • 785
  • 3
  • 7
  • 13

8 Answers8

59

You can use the "-r" option to set the sender address:

mailx -r me@example.com -s ...
sth
  • 222,467
  • 53
  • 283
  • 367
  • This is troubling for me because the man page of mailx says: "-r address Sets the From address. Overrides any from variable specified in environment or startup files. Tilde escapes are disabled. The -r address options are passed to the mail transfer agent unless SMTP is used. This option exists for compatibility only; it is recommended to set the from variable directly instead." ...specifically the trailing bit "This option exists for compatibility only....." – Pancho May 24 '22 at 19:29
45

The "-r" option is invalid on my systems. I had to use a different syntax for the "From" field.

-a "From: Foo Bar <foo.bar@someplace.com>"
Finch_Powers
  • 2,938
  • 1
  • 24
  • 34
  • 1
    In my case, onan Ubuntu 16.04, using your `-a` option, it does show the `Foo Bar` name in the received mail, but a reply to the received mail offers my gmail email address, used as smtp login, instead of the `foo.bar@someplace.com` one. – Stephane Jul 03 '17 at 13:48
  • 1
    This should be considered as a proper answer. – Denis V Apr 12 '18 at 10:01
  • 2
    i faced error: "From: Foo Bar " No such file or directory – Dee Jun 15 '18 at 04:10
  • I haven't tested this yet but when looking at man of mailx I see this: "-a file - Attach the given file to the message." ; which doesn't seem to line up neatly with "adding a From: header"? – Pancho May 24 '22 at 19:26
25

In case you also want to include your real name in the from-field, you can use the following format

mailx -r "me@example.com (My Name)" -s "My Subject" ...

If you happen to have non-ASCII characters in you name, like My AEÆoeøaaå (Æ= C3 86, ø= C3 B8, å= C3 A5), you have to encode them like this:

mailx -r "me@example.com (My =?utf-8?Q?AE=C3=86oe=C3=B8aa=C3=A5?=)" -s "My Subject" ...

Hope this can save someone an hour of hard work/research!

Rune
  • 698
  • 1
  • 7
  • 22
  • In case anyone's curious, the `=?utf-8?...?=` bit is a [MIME encoded-word](http://en.wikipedia.org/wiki/MIME#Encoded-Word). – Josh Kelley Apr 01 '14 at 13:25
  • The real name part of this doesn't work for me on solaris. The message successfully sends and has the proper email from, but each email I send the from is stripped down just to the email address. – peabody May 28 '14 at 17:45
  • @Rune Your method returns (My AE??oe??aa??) any idea why? – RafaSashi May 14 '15 at 12:27
  • @RafaSashi I'm guessing this is because your system is not set up to show UTF-8 characters (2-byte Norwegian characters in this case). On my Norwegian (UTF-8) system it reads "my AEÆoeøaaå" – Rune May 15 '15 at 14:27
  • Adding the `-a "Content-Type: text/plain; charset=UTF-8"` header solved my UTF-8 letters issue. Now the `é` is properly displayed. – Stephane Jul 03 '17 at 13:51
14

On debian where bsd-mailx is installed by default, the -r option does not work. However you can use mailx -s subject recipient@abc.com -- -f sender@abc.com instead. According to man page, you can specify sendmail options after --.

Marki555
  • 6,434
  • 3
  • 37
  • 59
  • Thanks for the tip, guess I should have read the synopsis in more detail. Unfortunately stuck with horrible bsdutils, glad to have a workaround. – 4ae1e1 Nov 18 '14 at 08:56
  • 5
    Unfortunately the [DSA 3104-1 security update](https://www.debian.org/security/2014/dsa-3104) broke this method. Instead, the heirloom-mailx package can be installed, which does provide a `-r` option. – praseodym Dec 17 '14 at 13:05
  • I just installed `bsd-mailx` version `Version: 8.1.2-0.20160123cvs-2`, albeit on an Ubuntu 16.04 system; the `-r` option works. – ssc Jan 29 '17 at 13:40
7

Just ran into this syntax problem on a CentOS 7 machine.

On a very old Ubuntu machine running mail, the syntax for a nicely composed email is

echo -e "$body" | mail -s "$subject" -a "From: Sender Name <$sender>" "$recipient"

However on a CentOS 7 box which came with mailx installed, it's quite different:

echo -e "$body" | mail -s "$subject" -S "from=Sender Name <$sender>" "$recipient"

Consulting man mail indicates that -r is deprecated and the 'From' sender address should now be set directly using -S "variable=value".

In these and subsequent examples, I'm defining $sender as "Sender Name <sender.address@domain.tld>" and $recipients as "recipient.name@domain.tld" as I do in my bash script.

You may then find, as I did, that when you try to generate the email's body content in your script at the point of sending the email, you encounter a strange behaviour where the email body is instead attached as a binary file ("ATT00001.bin", "application/octet-stream" or "noname", depending on client).

This behaviour is how Heirloom mailx handles unrecognised / control characters in text input. (More info: https://access.redhat.com/solutions/1136493, which itself references the mailx man page for the solution.)

To get around this, I used a method which pipes the generated output through tr before passing to mail, and also specifies the charset of the email:

echo -e "$body" | tr -d \\r | mail -s "$subject" -S "from=$sender" -S "sendcharsets=utf-8,iso-8859-1" "$recipients"

In my script, I'm also explicitly delaring the locale beforehand as it's run as a cronjob (and cron doesn't inherit environmental variables):

LANG="en_GB.UTF8" ; export LANG ;

(An alternate method of setting locales for cronjobs is discussed here)

More info on these workarounds via https://stackoverflow.com/a/29826988/253139 and https://stackoverflow.com/a/3120227/253139.

Chris Woods
  • 307
  • 3
  • 16
  • 1
    Note: The mailx package as used on RH-like distros (but also SLES) is actually the `heirloom mailx` which is more sophisticated than the `BSD mailx`. It has SMTP support and many different variables can be set via `-S` option. If you have a choice then go for `heirloom mailx`. – reichhart May 26 '20 at 00:12
  • Appreciate that detail @reichhart, thanks for commenting. – Chris Woods May 28 '20 at 00:57
2

The package nail provides an enhanced mailx like interface. It includes the -r option.

On Centos 5 installing the package mailx gives you a program called mail, which doesn't support the mailx options.

Neik
  • 43
  • 3
  • Note: The mailx package as used on RH-like distros (but also SLES) is actually the `heirloom mailx` which is more sophisticated than the `BSD mailx`. It has SMTP support and many different variables can be set via `-S` option. If you have a choice then go for `heirloom mailx`. – reichhart May 26 '20 at 00:13
1

On Ubuntu Bionic 18.04, this works as desired:

$ echo -e "testing email via yourisp.com from command line\n\nsent on: $(date)" | mailx --append='FROM:Foghorn Leghorn <fleghorn@yourisp.com>' -s "test cli email $(date)" -- recipient@acme.com

scrat.squirrel
  • 3,607
  • 26
  • 31
0

On macOS Sierra, creating ~/.mailrc with smtp setup did the trick:

set smtp-use-starttls
set smtp=smtp://smtp.gmail.com:587
set smtp-auth=login
set smtp-auth-user=youremail@gmail.com
set smtp-auth-password=yourpass

Then to send mail from CLI:

echo "your message" | mail -s "your subject" to_email@gmail.com
manitu
  • 1,189
  • 10
  • 16