Saturday 30 September 2017

How To Send Email From Linux Command Line

In Linux the mail command is an essential in our practical life. There is lot of instance when we want to send mail from Unix/Linux server to our outlook or personal email inbox. For example in Linux server we have generated csv file from database and want to pull this in our window machine. In this case we can use mail command to send file in our mailbox direct from Linux rather using FTP to pull report in local window machine.


Also mail command is useful in our automation script in which we want to generate some kind of report and send to its in recipients email box.

Before starting we should make sure we have installed mailx command in our system. If not installed run the following commands.

# For ubuntu/debian
$ sudo apt-get install heirloom-mailx

# For fedora/centos
$ sudo yum install mailx

Usage of mail command:
Once installation done you are ready to use mail command from command line. Following are the couple of examples .

1. Simple mail :
Run the following command and hit enter then you can write message for email body and for new line just hit enter and write message.
Once message complete press Ctrl+D and it would display EOT at the end.

Check the below message in recipient email if you not getting mail in inbox then check in spam/junk folder.

$ mail -s "This is subject" xyz@example.com
Hi there
This is a simple email body
bye!
EOT

2. Redirect message into mail from a file:

We can send email body message from a file by redirecting file as following.
$ mail -s "This is subject" xyz@example.com < /home/mukesh/message/message-body.txt
3. Message body in echo :

We can write the body message with echo as following.
$ echo -e "This is message body \nHere is second line." | mail -s "This is subject" xyz@example.com
4. Send mail to multiple recipients:

To send email to multiple recipient just separate the email id's  by comma.
echo -e "This is message body \nHere is second line." | mail -s "This is subject" xyz@example.com,abc@example.com
5. Using CC and BCC option:

Use -c to add CC and -b to add BCC
$ echo -e "This is message body" | mail -s "This is subject" -c ccrecipient@example.com xyz@example.com

$ echo -e "This is message body" | mail -s "This is subject" -c ccrecipient@example.com -b bccrecipient@example.com xyz@example.com
6. Specifying from Email:
If you wish to put sender email for recipient use -r option as following.

$ echo "This is message body" | mail -s "This is Subject" -r "Mukesh<fromemail@example.com>" recipient@example.com
7.Email with attachment ( Important ):

To attach  file use -a option as following.
echo "This is Message Body" | mail -s "This is Subject" -a /home/mukesh/message/sampleFile.csv recipient@example.com

echo "This is Message Body" | mail -s "This is Subject" -r "Mukesh<fromemail@example.com>" -a /home/mukesh/message/sampleFile.csv recipient@example.com


**** End****

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...