112

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

1
  • Reading the manual page would be a good starting point. Commented May 29, 2024 at 10:18

10 Answers 10

124

an example

$ echo "something" | mailx -s "subject" [email protected]

to send attachment

$ uuencode file file | mailx -s "subject" [email protected]

and to send attachment AND write the message body

$ (echo "something\n" ; uuencode file file) | mailx -s "subject" [email protected]
Sign up to request clarification or add additional context in comments.

5 Comments

I tried it but it is givinng no response. Neither it is giving some error msg nor sending the mail to [email protected]. Do there is need for any server configuration?
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.
You should also check for error messages in your inbox. I.e. run mail.
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.
@user269484 Gmail doesn't accept email from unauthorised IP addresses. Read support.google.com/mail/answer/10336
39

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.

2 Comments

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
you could do -S [email protected]
8
mailx -s "subjec_of_mail" [email protected] < 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

2 Comments

This doesn't attach the file, it puts the content of the file into the body
mailx: cannot send message: Process exited with a non-zero status
8

Its faster with MUTT command

echo "Body Of the Email"  | mutt -a "File_Attachment.csv" -s "Daily Report for $(date)"  -c [email protected] [email protected] -y
  1. -c email cc list
  2. -s subject list
  3. -y to send the mail

1 Comment

I had to separate attachments with "--" to make it work like this: echo "" | mutt -s "test" -a ./file -- [email protected] -y
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.

Comments

5

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" [email protected]

Comments

3
echo "Sending emails ..."
NOW=$(date +"%F %H:%M")
echo $NOW  " Running service" >> open_files.log
header=`echo "Service Restarting: " $NOW`


mail -s "$header" [email protected],   \
              [email protected], \ < open_files.log

Comments

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

Comments

1

Customizing FROM address

MESSAGE="SOME MESSAGE"
SUBJECT="SOME SUBJECT"
TOADDR="[email protected]"
FROM="DONOTREPLY"

echo $MESSAGE | mail  -s "$SUBJECT" $TOADDR  -- -f $FROM

2 Comments

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.
It depends on which version you are using. There are multiple incompatible competing mail and mailx commands with different options.
0

If you want to send more than two person or DL :

echo "Message Body" | mailx -s "Message Title" -r [email protected] [email protected],[email protected]

here:

  • -s = subject or mail title
  • -r = sender mail or DL

2 Comments

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.
It is awaiting your input.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.