Mail Command

Mail command is a very useful command in Linux to send a mail through terminal.

CentOS 7

$ sudo yum update -y
$ sudo yum install mailx -y
$ sudo mail -V
$ echo "This is test email" | mail -s "Test Email" root@localhost
$ tail -f /var/spool/mail/root

Instructions

Send mail to a local system user

$ mail -s "Hello World" username

Subject and Message in a single line

$ mail -s "Hello World" someone@example.com
$ mail -s "This is the subject" somebody@example.com <<< 'This is the message'
$ echo "This is the body" | mail -s "Subject" -aFrom:Harry\<harry@gmail.com\> someone@example.com

Take message from a file

$ mail -s "Hello World" user@yourmaildomain.com < /home/user/mailcontent.txt
$ echo "This is the message body" | mail -s "This is the subject" mail@example.com

Specify CC and BCC recipients

$ mail -s "Hello World" user1@example.com -c usertocc@example.com -b usertobcc@example.com

Sending to multiple recipients

$ mail -s "Hello World" user1@example.com,user2@example.com

Specify the FROM name and address

$ echo "This is the message body" | mail -s "This is the subject" mail@example.com -aFrom:sender@example.com
$ echo "This is the body" | mail -s "Subject" -aFrom:Harry\<harry@gmail.com\> someone@example.com
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus