home   articles   tags   browse code   

BASH - Linux sendmail script


 

Most linux distributions come with the sendmail application. Some interpreted languages like php use it for its mail() function. I have even seen command line php used from bash for its mail functions as opposed to the command line sendmail. When you are bash scripting, it is often better to use sendmail directly as opposed to adding the overhead, complexity and extra dependency (for portability) of php.

Here is how to send a simple email using linux sendmail, for use in a cronjob script. These are generally used for administrative purposes, so a text-only email is usually just fine. We also have an article about using bash and sendmail with an attachment.


#!/bin/bash
#requires: date,sendmail
function fappend {
    echo "$2">>$1;
}
YYYYMMDD=`date +%Y%m%d`

# CHANGE THESE
TOEMAIL="recipient@email.com";
FREMAIL="crondaemon@65.101.11.232";
SUBJECT="Daily Backup - $YYYYMMDD";
MSGBODY="This is your daily backup notice";

# DON'T CHANGE ANYTHING BELOW
TMP="/tmp/tmpfil_123"$RANDOM;

rm -rf $TMP;
fappend $TMP "From: $FREMAIL";
fappend $TMP "To: $TOEMAIL";
fappend $TMP "Reply-To: $FREMAIL";
fappend $TMP "Subject: $SUBJECT";
fappend $TMP "";
fappend $TMP "$MSGBODY";
fappend $TMP "";
fappend $TMP "";
cat $TMP|sendmail -t;
rm $TMP;


Note: when i put this in a cronjob I usually have to replace 'sendmail' above with '/usr/sbin/sendmail'. To find out where sendmail is on your system type:
[root@server /home] which sendmail


 

 
James Randal on Feb 3rd, 2010 12:20 am said:
what is the rm -rf for ?? ;)
 

alex on Mar 23rd, 2010 10:10 am said:
instead of TMP="/tmp/tmpfil_123"$RANDOM; you could have written TMP=`mktemp`
 

Dama on Jul 28th, 2010 3:52 pm said:
How can I send a HTML email using the above script?
 

Taj on Feb 3rd, 2011 11:53 am said:
Thank you so much !!!!!!!!!!!. This really helped me. You rock !!!!!
 

Chris on Mar 18th, 2011 2:50 am said:
Maybe this is useful for anyone (sorry for the linebreak issues in this comment...): If you want to use linebreaks in $MSGBODY, a simple n won't do (at least it didn't for me). If you hardcode the variable in your script, just press Enter instead of inserting a n -> MSGBODY="Hi there, I just wanted to tell you... bye" If this doesn't satisfy your eyes because of the formatting of your script, there's another way: MSGBODYTXT="Hi there,nnI just wanted to tell you...nnbye"; MSGBODY=`echo -e "$MSGBODYTXT"` Of course this also works out well if you use a script's parameter instead of $MSGBODYTXT - e.g. in combination with a sort of signature: (ran the script with 1st parameter "Hi therennblabla") SIGTXT="nnConcernedly YoursnnSimona Silly" MSGBODY=`echo -e "$1" "$SIGTXT"`
 

Chris on Mar 18th, 2011 2:52 am said:
imagine any "n" or "nn" that doesn't make sense with an ESCAPE before it...
 



 

 



Related Articles
 




home  |  privacy policy  |  terms of use  |  contact  


©2012, Zedwood.com

zedwood.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com