[shell] How to set the From email address for mailx command?

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?

This question is related to shell email solaris ksh mailx

The answer is


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 <[email protected]>' -s "test cli email $(date)" -- [email protected]


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 <[email protected]>" and $recipients as "[email protected]" 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.


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.


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

mailx -r "[email protected] (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 "[email protected] (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!


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 [email protected]
set smtp-auth-password=yourpass

Then to send mail from CLI:

echo "your message" | mail -s "your subject" [email protected]

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

-a "From: Foo Bar <[email protected]>"

On debian where bsd-mailx is installed by default, the -r option does not work. However you can use mailx -s subject [email protected] -- -f [email protected] instead. According to man page, you can specify sendmail options after --.


Examples related to shell

Comparing a variable with a string python not working when redirecting from bash script Get first line of a shell command's output How to run shell script file using nodejs? Run bash command on jenkins pipeline Way to create multiline comments in Bash? How to do multiline shell script in Ansible How to check if a file exists in a shell script How to check if an environment variable exists and get its value? Curl to return http status code along with the response docker entrypoint running bash script gets "permission denied"

Examples related to email

Monitoring the Full Disclosure mailinglist require(vendor/autoload.php): failed to open stream Failed to authenticate on SMTP server error using gmail Expected response code 220 but got code "", with message "" in Laravel How to to send mail using gmail in Laravel? Laravel Mail::send() sending to multiple to or bcc addresses Getting "The remote certificate is invalid according to the validation procedure" when SMTP server has a valid certificate How to validate an e-mail address in swift? PHP mail function doesn't complete sending of e-mail How to validate email id in angularJs using ng-pattern

Examples related to solaris

How to get rid of the "No bootable medium found!" error in Virtual Box? What is the reason and how to avoid the [FIN, ACK] , [RST] and [RST, ACK] sed edit file in place Windows equivalent of linux cksum command How to use S_ISREG() and S_ISDIR() POSIX Macros? Test if remote TCP port is open from a shell script Portable way to get file size (in bytes) in shell? Get the date (a day before current time) in Bash How to set the From email address for mailx command? How to determine the IP address of a Solaris system

Examples related to ksh

How can I find a file/directory that could be anywhere on linux command line? Shell Script: How to write a string to file and to stdout on console? Send password when using scp to copy files from one server to another Reading file line by line (with space) in Unix Shell scripting - Issue How to get the second column from command output? Get exit code for command in bash/ksh Check if file exists and whether it contains a specific string In a unix shell, how to get yesterday's date into a variable? How to set the From email address for mailx command? How to mkdir only if a directory does not already exist?

Examples related to mailx

Mailx send html message Sending simple message body + file attachment using Linux Mailx How can I send an email through the UNIX mailx command? How to set the From email address for mailx command?