Bash Script to send Email in Linux

Hi Guys,

If you have mail command available then using it in a shell script is not that difficult, If a guy like me can do it anyone can I guess because I am not a native Linux man 🙂

Anyway lets check how to do it

I saved above as

As you will notice that I am using the heredoc syntax in the above script.

There are sometimes that you want to send custom email messages so accepting the message from command line param makes sense so here is a little modification to the above script so that you can make use of it if you think that it could be useful


So as you can see that in the MSG container I’ve use ${1} bit which means that the first command param becomes my email message as shown below

That makes sense I guess.

This is just a basic bash script, the Linux Gurus reading this post will have more to share if they wish to leave their feedback.

One last bit, do not forget to give execute permission to the current user else you will not be able to use above bash script.

I hope this will help or at least give you an indicator that will serve towards your final result/requirement


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.