Mail command is a very useful command in Linux to send a mail through terminal.
CentOS 7
1
2
3
4
5
$ 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
1
$ mail -s "Hello World" username
Subject and Message in a single line
1
2
3
$ 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
1
2
$ 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
1
$ mail -s "Hello World" user1@example.com -c usertocc@example.com -b usertobcc@example.com
Sending to multiple recipients
1
$ mail -s "Hello World" user1@example.com,user2@example.com
Specify the FROM name and address
1
2
$ 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