How To Send Mail Using Mail Command in UNIX

Sometimes we need to send e-mails from UNIX command-line itself. Sometimes we need to write a small script to do so. Here’s what we need to do for such tasks.

We can send mail in UNIX using “mail” command. Syntax is as follows,

mail -s "Subject" -c "any_cc_email_id" -b "any_bc_email_id" "comma_separated_To_email_id"

But the above syntax is used when we are typing the message body at command-line itself.

If we want to use the body text from some variable or from some pipe (|) we need to use following syntax,

echo $message | mail -s "Subject" -c "any_cc_email_id" -b "any_bc_email_id" "comma_separated_To_email_id"


echo "Hi Friend, this is mail body" | mail -s "Hi Friend" ","

If we want to get the message body from a file rather than from a variable, we need to following syntax

mail -s "Subject" -c "any_cc_email_id" -b "any_bc_email_id" "comma_separated_To_email_id" < file.txt


mail -s "Hi Friend" "," < myfile.log

Lot of programmers ask about how to send this mail to multiple users. Please note that they need to put comma separated email ids and not space separated ones in the above syntax (as I have already shown).

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>