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
1 2 3 4 5 6 7 8 9 |
#!/bin/bash # set the subject SUBJECT="THIS IS MY EMAIL SUBJECT" # set the recipient email address EMAIL="jas@myemailaddress" #send the message /bin/mail -s "$SUBJECT" "$EMAIL" <<MSG this is my test message MSG |
I saved above as jasmail.sh
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
Alternative
1 2 3 4 5 6 7 8 9 |
#!/bin/bash # set the subject SUBJECT="THIS IS MY EMAIL SUBJECT" # set the recipient email address EMAIL="jas@myemailaddress" #send the message /bin/mail -s "$SUBJECT" "$EMAIL" <<MSG ${1} MSG |
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
1 |
[jas@webs ~]# ./jasmail.sh "This is my email message" |
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
Cheers
Leave a Reply