Press "Enter" to skip to content

email from bash script

Got a need to automatically email a copy of a log output from a another script?  Easily accomplished with this little snippet

[bash]
#!/bin/bash
# script to send simple email
# email subject
SUBJECT="This is a real email"
# Email To ?
EMAIL="email@mydomain.com"

# Email text/message
EMAILMESSAGE="/tmp/log"

#echo "This is email text" >>$EMAILMESSAGE

# send an email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
[/bash]

I have piles of different scripts to perform tasks for me at all hours of the day and night.  Since I don’t want babysit them to be sure they ran, or did what I expected I simply dump the output I want to a file (/tmp/log) in the example above, and add this to the end of my script… automagically there is an email waiting for me to review what happened at my leisure.

Another good use is for text message alerts for something I would consider important…. Intrusion Detection System sees something that needs immediate attention…or a checksum unexpectedly changes for a critical file on your website, say the main index.php changes unexpectedly….. I want to know NOW, so send an email to my cell as a text message.  Most cell phone companies have an email to text gateway alias set up where you can send an email to 1234567890@txt.att.net (for AT&T) and it is sent as a text or mms message to the number listed.