Linux Mail Command Usage with Examples
Have you imagined to send emails through command line from a server or automate an email with all the shell scripting or web applications? Linux servers facilitate the ability to send emails from the command line or ¬†by scripting. In this tutorial we will be discussing on how to use the mail commands on Linux in order to send emails through terminal.
Working of mail command
Whenever a mail is sent, initially the mail command calls the standard send mail binary which is located in /usr/sbin/sendmail. This call will connect the local Mail Transfer Agent (MTA) into sending the email to the destination. The Simple Mail Transfer Protocol (SMPT) server on port 25 which is locally running Mail Transfer Agent (MTA) will receive the emails.
Error message like ‚Äúsend-mail: Cannot open mail: 25‚ÄĚ occur if Simple Mail Transfer Protocol (SMTP) server like Postfix is not available in the sender machine.
Installation of mail command
Installation of mail command can be accessed from different packages. Following are the list:
1) gnu mailutils
It is to be noted that the features and options included in the packages are different from one another. In this case we have heirloom-mailx package which enables the connection of an external Simple Mail Transfer Protocol (SMTP) server to send messages. The other two packages are used to connect only local Simple Mail Transfer Protocol (SMTP) server. In this tutorial we will be discussing on heirloom-mailx package.
Install mail command on CentOS/Redhat:
$ yum install mailx -y
The following command will allow you to send an email with the subject. The option ‚Äės‚Äô allow you to an send email with subject followed by the recipient address.
$ mail -s ‚ÄúEnter the subject‚ÄĚ [email protected]
Don‚Äôt you think something important is missing, the message! The next step before pressing the enter buttons is to type the message or if any CC (Carbon Copy) required.
After the completion of required fields press Ctrl + D, this will give an output EOT.
Send message from a file
The content of the message can be included from a file also.
$ mail -s ‚ÄúThe Subject‚ÄĚ [email protected] < /root/testemail.txt
Messages can also be including by using echo command and piping it.
$ echo ‚ÄúTHIS IS A TEST EMAIL‚ÄĚ | mail -s ‚ÄúEnter the subject‚ÄĚ¬†[email protected]
We do also have option to send the email to multiple recipients.
$ echo ‚ÄúTHIS IS A TEST EMAIL‚ÄĚ | mail -s ‚ÄúEnter the subject‚ÄĚ¬†[email protected],[email protected]
CC and BCC
Blind carbon copy (BCC) and carbon copy (CC) address can also be attached within a command in Linux.
$ echo ‚ÄúTHIS IS A TEST EMAIL‚ÄĚ | mail -s ‚ÄúEnter the subject‚ÄĚ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† -c¬†[email protected],[email protected]
Specify From name and address
$ echo ‚ÄúTHIS IS A TEST EMAIL‚ÄĚ | mail -s ‚ÄúEnter the subject‚ÄĚ ‚Äďr ‚ÄúTom<[email protected]>‚ÄĚ¬†[email protected]
Specify ‚ÄúReply-To‚ÄĚ address
$ echo ‚ÄúTHIS IS A TEST EMAIL‚ÄĚ | mail -s ‚ÄúEnter the subject‚ÄĚ replyto=¬†[email protected]¬†[email protected]
$ echo ‚ÄúTHIS IS A TEST EMAIL‚ÄĚ | mail -s ‚ÄúEnter the subject‚ÄĚ replyto= ‚ÄúTom[email protected]‚ÄĚ¬†[email protected]
The option ‚Äďa is used to add an attachment along with the email.
$ echo ‚ÄúTHIS IS A TEST EMAIL‚ÄĚ | mail -s ‚ÄúEnter the subject‚ÄĚ ‚Äďr ‚ÄúTom<[email protected]>‚ÄĚ ‚Äďa /path/to/file¬†[email protected]
If you need any further assistance please contact our support department.