

Send mail with a BASH Shell Script
source link: https://www.devroom.io/2007/06/10/send-mail-with-a-bash-shell-script/?utm_campaign=Feed%3A+ariejan+%28ariejan%7Cdevroom.io%29
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Send mail with a BASH Shell Script
Posted:
2007-06-10
- Last updated:
2019-06-05
Tagged general features bash shell scripts
Like any good programmer, I try to automate the crap out of everything. If you have to do it more than once, I try to write a script for it.
This time I want to show you how you can easily send an e-mail from a BASH script. The idea is that you want the script to send out an email to notify a user that something has happened.
We’re going to use the GNU Mail utility here. The basic syntax to send an email is like this:
/usr/bin/mail -s "Subject" [email protected] < message.txt
The trick when using this in a shell script is creating and using the message.txt file correctly.
Let’s setup the basis first:
#!/bin/bash SUBJECT="Automated Security Alert" TO="[email protected]" MESSAGE="/tmp/message.txt" /usr/bin/mail -s "$SUBJECT" "$TO" < $MESSAGE
All we need to do now is create the message. In this example we’re going to notify the receiver that something happened at a certain time. We can use the append (») operator to add text to the message file. Afterwards, we must remove the temporary message file, of course. The complete script now becomes:
#!/bin/bash SUBJECT="Automated Security Alert" TO="[email protected]" MESSAGE="/tmp/message.txt" echo "Security breached!" >> $MESSAGE echo "Time: `date`" >> $MESSAGE /usr/bin/mail -s "$SUBJECT" "$TO" < $MESSAGE rm $MESSAGE
The email will contain the a timestamp from when the mail was sent.
This method is great for letting an administrator now if something happened. Maybe you need to check if your webserver is up and running. This script can an administrator about the issue.
Recommend
-
7
Unable to send mail iPhone SE S Cee macrumors member Original poster
-
12
How to send an 'E mail' - Database - 19845,790,971 views•Feb 26, 2016 ...
-
6
In django we can send e-mail using SMTP (Simple Mail Transfer Protocol) service which is inbuilt package in python and also in django. What is smtp ? SMTP (Simple Mail Transfer Pro...
-
16
Unable to send e-mail to laravel 5.2 advertisements I am trying to send an email with laravel 5.2 . This is my first time of sending email in...
-
15
How do I send PHP mail via SMTP? Overview When you use the PHP mail function, you are sending...
-
29
Php contact form does not send e-mail advertisements This question already has an answer here: PHP mail function doesn't complete...
-
5
Phpmailer to send e-mail from any gmail advertisements I used the following code snippet using phpmailer class inorder to send an email using gmail...
-
8
What have people used for an E-mail send relay? Floodgap may be relocating, which may mean if I can’t preserve the IP that I’d lose over a decade of accumulated good karma (I do have an SPF and have never been on any majo...
-
11
Send an e-mail from a script in batches. Blat does not work advertisements I need to send an email from a batch script. I tried several soluti...
-
11
Bash script to send a push notification when string is found in logs ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK