104

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

zb226
  • 9,586
  • 6
  • 49
  • 79
user269484
  • 1,093
  • 2
  • 9
  • 5

10 Answers10

116

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
Bernardo0
  • 3
  • 3
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • 4
    I 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
  • 2
    there 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
  • 3
    You 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
  • 3
    Note, 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
35

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.

james.garriss
  • 12,959
  • 7
  • 83
  • 96
Kamran Ahmad
  • 351
  • 3
  • 2
  • 1
    On 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
  • you could do -S from=a@b.com – Kalpesh Soni Jun 08 '17 at 18:54
9
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

Girdhar Singh Rathore
  • 5,030
  • 7
  • 49
  • 67
6

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
  1. -c email cc list
  2. -s subject list
  3. -y to send the mail
Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
user1651561
  • 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
6
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.

Pavan Kumar
  • 127
  • 1
  • 8
4

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
hlovdal
  • 26,565
  • 10
  • 94
  • 165
3
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
Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56
2

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

Ivo Yordanov
  • 146
  • 1
  • 8
1

Customizing FROM address

MESSAGE="SOME MESSAGE"
SUBJECT="SOME SUBJECT"
TOADDR="u@u.com"
FROM="DONOTREPLY"

echo $MESSAGE | mail  -s "$SUBJECT" $TOADDR  -- -f $FROM
Hari
  • 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
0

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
dildeepak
  • 1,349
  • 2
  • 16
  • 34