How can I send an email through the UNIX mailx command?
UnixEmailMailxUnix Problem Overview
How can I send an email through the UNIX mailx
command?
Unix Solutions
Solution 1 - Unix
an example
$ echo "something" | mailx -s "subject" [email protected]
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" [email protected]
Solution 2 - Unix
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.
Solution 3 - Unix
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
Solution 4 - Unix
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.
Solution 5 - Unix
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
- -c email cc list
- -s subject list
- -y to send the mail
Solution 6 - Unix
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]
Solution 7 - Unix
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
Solution 8 - Unix
Customizing FROM address
MESSAGE="SOME MESSAGE"
SUBJECT="SOME SUBJECT"
TOADDR="[email protected]"
FROM="DONOTREPLY"
echo $MESSAGE | mail -s "$SUBJECT" $TOADDR -- -f $FROM
Solution 9 - Unix
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
Solution 10 - Unix
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