How can I send an email through the UNIX mailx
command?

- 9,586
- 6
- 49
- 79

- 1,093
- 2
- 9
- 5
10 Answers
an example
$ echo "something" | mailx -s "subject" recipient@somewhere.com
to send attachment
$ uuencode file file | mailx -s "subject" recipient@somewhere.com
and to send attachment AND write the message body
$ (echo "something\n" ; uuencode file file) | mailx -s "subject" recipient@somewhere.com

- 3
- 3

- 327,991
- 56
- 259
- 343
-
4I tried it but it is givinng no response. Neither it is giving some error msg nor sending the mail to myname@gmail.com. Do there is need for any server configuration? – user269484 Feb 18 '10 at 04:58
-
2there is no need for any configuration. Check your internet connection. I have a direct connection to the internet through cable, I don't use proxies or anything so it works on my side. – ghostdog74 Feb 18 '10 at 05:58
-
3You should also check for [error messages](http://www.unix.com/unix-advanced-expert-users/8741-mailx-error.html) in your inbox. I.e. run `mail`. – hafichuk Dec 31 '12 at 05:52
-
3Note, however, that `uuencode` is a legacy technology from a bygone millennium which does not produce what we today mean by "attachments". It basically puts a machine-readable piece of jumble at the end of the message text. In this day and age, you would be better served by a properly MIME-aware mailer. Unfortunately, there is no universally supported `mailx` replacement with MIME features, but if you have `mutt`, that's probably the path of least resistance. – tripleee Oct 01 '14 at 03:18
-
5@user269484 Gmail doesn't accept email from unauthorised IP addresses. Read https://support.google.com/mail/answer/10336 – Jan 11 '16 at 18:07
Here you are :
echo "Body" | mailx -r "FROM_EMAIL" -s "SUBJECT" "To_EMAIL"
PS. Body and subject should be kept within double quotes.
Remove quotes from FROM_EMAIL
and To_EMAIL
while substituting email addresses.

- 12,959
- 7
- 83
- 96

- 351
- 3
- 2
-
1On Mac you will receive an error from the mailx command if you use -r mailx: illegal option -- r Usage: mailx [-EiInv] [-s subject] [-c cc-addr] [-b bcc-addr] [-F] to-addr ... mailx [-EHiInNv] [-F] -f [name] mailx [-EHiInNv] [-F] [-u user] mailx -e [-f name] mailx -H – jcpennypincher Apr 15 '16 at 19:36
-
mailx -s "subjec_of_mail" abc@domail.com < file_name
through mailx
utility we can send a file from unix
to mail server
.
here in above code we can see
first parameter is -s "subject of mail"
the second parameter is mail ID
and the last parameter is name of file which we want to attach

- 5,030
- 7
- 49
- 67
-
2This doesn't attach the file, it puts the content of the file into the body – Guus Dec 17 '18 at 16:58
-
Its faster with MUTT command
echo "Body Of the Email" | mutt -a "File_Attachment.csv" -s "Daily Report for $(date)" -c cc_mail@g.com to_mail@g.com -y
- -c email cc list
- -s subject list
- -y to send the mail

- 6,602
- 11
- 44
- 59

- 197
- 3
- 3
-
I had to separate attachments with "--" to make it work like this: `echo "" | mutt -s "test" -a ./file -- name@mail.com -y` – subtleseeker Oct 22 '20 at 07:36
mail [-s subject] [-c ccaddress] [-b bccaddress] toaddress
-c and -b are optional.
-s : Specify subject;if subject contains spaces, use quotes.
-c : Send carbon copies to list of users seperated by comma.
-b : Send blind carbon copies to list of users seperated by comma.
Hope my answer clarifies your doubt.

- 127
- 1
- 8
From the man page:
Sending mail
To send a message to one or more people, mailx can be invoked with arguments which are the names of people to whom the mail will be sent. The user is then expected to type in his message, followed by an ‘control-D’ at the beginning of a line.
In other words, mailx reads the content to send from standard input and can be redirected to like normal. E.g.:
ls -l $HOME | mailx -s "The content of my home directory" someone@email.adr

- 26,565
- 10
- 94
- 165
echo "Sending emails ..."
NOW=$(date +"%F %H:%M")
echo $NOW " Running service" >> open_files.log
header=`echo "Service Restarting: " $NOW`
mail -s "$header" abc.xyz@google.com, \
cde.mno@yahoo.com, \ < open_files.log

- 15,454
- 6
- 47
- 56
Here is a multifunctional function to tackle mail sending with several attachments:
enviaremail() {
values=$(echo "$@" | tr -d '\n')
listargs=()
listargs+=($values)
heirloom-mailx $( attachment=""
for (( a = 5; a < ${#listargs[@]}; a++ )); do
attachment=$(echo "-a ${listargs[a]} ")
echo "${attachment}"
done) -v -s "${titulo}" \
-S smtp-use-starttls \
-S ssl-verify=ignore \
-S smtp-auth=login \
-S smtp=smtp://$1 \
-S from="${2}" \
-S smtp-auth-user=$3 \
-S smtp-auth-password=$4 \
-S ssl-verify=ignore \
$5 < ${cuerpo}
}
function call: enviaremail "smtp.mailserver:port" "from_address" "authuser" "'pass'" "destination" "list of attachments separated by space"
Note: Remove the double quotes in the call
In addition please remember to define externally the $titulo (subject) and $cuerpo (body) of the email prior to using the function

- 146
- 1
- 8
Customizing FROM address
MESSAGE="SOME MESSAGE"
SUBJECT="SOME SUBJECT"
TOADDR="u@u.com"
FROM="DONOTREPLY"
echo $MESSAGE | mail -s "$SUBJECT" $TOADDR -- -f $FROM

- 631
- 7
- 5
-
An except from man mail: -f [file] Read in the contents of the user's mbox (or the specified file) for processing; when mailx is quit, it writes undeleted messages back to this file. The string file is handled as described for the folder command below. – ZJ Lyu May 13 '17 at 03:48
-
It depends on which version you are using. There are multiple incompatible competing `mail` and `mailx` commands with different options. – tripleee Aug 14 '19 at 14:09
If you want to send more than two person or DL :
echo "Message Body" | mailx -s "Message Title" -r sender@someone.com receiver1@someone.com,receiver_dl@.com
here:
- -s = subject or mail title
- -r = sender mail or DL

- 1,349
- 2
- 16
- 34
-
Can you please explain why echo is used to pass a message body? If I omit this and execute from MailX, the cursor blinks as if awaiting an input. – LonelyRogue Apr 22 '20 at 15:57
-