Shell Script To E-Mail

Hi All,

I'm having some problems with my shell script. When running the script I get the following errors:

line 101: <html>: command not found
line 105: /dumpfile.txt: No such file or directory

The file dumpfile.txt does exist and I have double and tripple checked this. I'm not sure what I am doign wrong with the HTML tag part here as I want it to attach an html file. Any advice please?

#!/bin/bash
# ===================================================================== #
#     OTRS BACKUP SCRIPT WITH SYSTEM INFORMATION    #
#       SCRIPT CREATED 15/11/2012      #
#      SCRIPT LAST MODIFIED 15/11/2012      #
# ===================================================================== #

# ===================================================================== #
#      VARIABLES THAT CAN BE USED IN THE SCRIPT     #
# ===================================================================== #

# CREATES A TITLE VARIABLE
TITLE="OTRS System Status"
# GATHERS THE SYSTEM TIMESTAMP INFORMATION
SYS_DATE=$(date)
# SPECIFY THE CONTENT OF THE E-MAIL TO BE SENT
MAILCONTENT="files"
# SETS THE MAXIMUM SIZE OF THE E-MAIL 4000 = +/- 5MB
MAXATTSIZE="4000"
# E-MAIL ADDRESS WHERE THE E-MAIL WILL BE SENT
MAILADDR="email@domain.com"
# DEFINE THE E-MAIL BODY
SUBJECTFILE=$BACKUPDIR/dumpfile.txt

# ===================================================================== #
#        FUNCTIONS         #
#      STUBBING IS USED AS A TEMPORY PLACE HOLDER FOR FUTURE CODE       #
# ===================================================================== #

function mysql_dump
{
 echo "<pre>"
 mysqldump db1 > db1.sql --user=user --password=password &&  mysqldump db2 > db2.sql --user=user --password=password
 echo "<pre>"
}
# ---- END OF MYSQL DUMP ---- #

function drive_space
{
    echo "<h2>Filesystem Space</h2>"
    echo "<pre>"
    df -h
    echo "</pre>"
}
# ---- END OF DRIVE SPACE ---- #

function backup_size
{
    echo "<h2>Backup Size</h2>"
    echo "<pre>"
    du -h *.sql
    echo "</pre>"
}
# ---- END OF BACKUP SIZE ---- #

function additional_function
{
    # TEMPORARY FUNCTION STUB
    echo "function additional_function"
}
# ---- END OF ADDITIONAL FUNCTIONS ---- #

# ===================================================================== #
#     EXECUTING MYSQL_DUMP FUNCTION       #
# ===================================================================== #
$(mysql_dump)

# ===================================================================== #
#     GATHERS AND DISPLAYS THE STATUS OF THE SYSTEM   #
# ===================================================================== #
function attachment
{
cat <<- _OTRSSTATUS_
  <html>
  <head>
  <title>$TITLE</title>
  </head>
  
  <body>
  <h1>$TITLE</h1>
  <p></p>
  $(drive_space)
  $(backup_size)
  <body>
  </html>
_OTRSSTATUS_
}

# ===================================================================== #
#    GATHERS INFORMATION AND SEND MESSAGE ATTACHMENT    #
# ===================================================================== #
$(attachment)
if [ "$MAILCONTENT" = "files" ]
 then
 mutt -s "System Status $DATE" -a $(attachment) -- $MAILADDR < $SUBJECTFILE
fi

exit 0
 
 
 
 

Hi, you cannot use command substitution like this:

$(mysql_dump)

or

$(attachment)

This will try to execute the result of the substitution...

1 Like

Ok, can you maybe help me get it working? I have manage to fix the first few errors and now I get the following:

Can't stat <html>: No such file or directory
<html>: unable to attach file.

Try using double quotes around variable references, for example "$(attachment)"

I tried this, and now it prints the output of the shell script. However it still doesn't attach and e-mail the file.

At the start of the print screen I get

Can't Stat

At the end I get

</html>: unable to attach file.

---------- Post updated at 03:34 PM ---------- Previous update was at 02:02 PM ----------

I have managed to get it to actually attach the file. But without and contents.

#!/bin/bash
# ===================================================================== #
# OTRS BACKUP SCRIPT WITH SYSTEM INFORMATION #
# SCRIPT CREATED 15/11/2012 #
# SCRIPT LAST MODIFIED 15/11/2012 #
# ===================================================================== #

# ===================================================================== #
# VARIABLES THAT CAN BE USED IN THE SCRIPT #
# ===================================================================== #
# DIRECTORY WHERE FILES ARE STORED
BACKUPDIR="/mnt/data6/reports"
# CREATES A TITLE VARIABLE
TITLE="System Status"
# SPECIFY THE CONTENT OF THE E-MAIL TO BE SENT
MAILCONTENT="files"
# SETS THE MAXIMUM SIZE OF THE E-MAIL 4000 = +/- 5MB
MAXATTSIZE="4000"
# E-MAIL ADDRESS WHERE THE E-MAIL WILL BE SENT
MAILADDR=mymail@domain.com
# DEFINE THE E-MAIL BODY
SUBJECTFILE=$BACKUPDIR/sqldump.txt
# CREATE THE FILE TO ATTACH
ATTACHMENT=$BACKUPDIR/Status.html

# ===================================================================== #
# FUNCTIONS #
# STUBBING IS USED AS A TEMPORY PLACE HOLDER FOR FUTURE CODE #
# ===================================================================== #




function drive_space
{
echo "<h2>Filesystem Space</h2>"
df -h
}
# ---- END OF DRIVE SPACE ---- #

function backup_size
{
echo "<h2>Backup Size</h2>"
du -h sqldump.sh
}
# ---- END OF BACKUP SIZE ---- #

function additional_function
{
# TEMPORARY FUNCTION STUB
echo "function additional_function"
}
# ---- END OF ADDITIONAL FUNCTIONS ---- #




# ===================================================================== #
# GATHERS AND DISPLAYS THE STATUS OF THE SYSTEM #
# ===================================================================== #
function ATTACHMENT
{
cat <<-_Status_
<html>
<head>
<title>$TITLE</title>
</head>

<body>
<h1>$TITLE</h1>
<p></p>
$(drive_space)
$(backup_size)
<body>
</html>
_Status_ > $ATTACHMENT
}
# ===================================================================== #
# GATHERS INFORMATION AND SEND MESSAGE ATTACHMENT #
# ===================================================================== #


if [ "$MAILCONTENT" = "files" ]
then
mutt -s "System Status $DATE" -a $ATTACHMENT -- $MAILADDR < $SUBJECTFILE
fi

exit 0

Hi, you need redirect on the first line:

cat <<-_Status_ > file

You would also need to call the function..

I still seem to be having a problem with the attachment actually containing any data. The script runs sending an e-mail with an HTML file attached, however when I open the HTML file it is blank. Below is what my script currently looks like.

#!/bin/bash
# ===================================================================== #
#      BACKUP SCRIPT WITH SYSTEM INFORMATION    #
#       SCRIPT CREATED 15/11/2012      #
#      SCRIPT LAST MODIFIED 16/11/2012      #
# ===================================================================== #
 
# ===================================================================== #
#      VARIABLES THAT CAN BE USED IN THE SCRIPT     #
# ===================================================================== #
# DIRECTORY WHERE FILES ARE STORED
BACKUPDIR="/mnt/data6/reports"
# CREATES A TITLE VARIABLE
TITLE=" System Status"
# SPECIFY THE CONTENT OF THE E-MAIL TO BE SENT
MAILCONTENT="files"
# SETS THE MAXIMUM SIZE OF THE E-MAIL 4000 = +/- 5MB
MAXATTSIZE="4000"
# E-MAIL ADDRESS WHERE THE E-MAIL WILL BE SENT
MAILADDR="myemail@mydomain.com"
# DEFINE THE E-MAIL BODY
SUBJECTFILE=$BACKUPDIR/sqldump.txt
# CREATE THE FILE TO ATTACH
ATTACHMENT=$BACKUPDIR/Status.html
 
# ===================================================================== #
#        FUNCTIONS         #
#      STUBBING IS USED AS A TEMPORY PLACE HOLDER FOR FUTURE CODE       #
# ===================================================================== #
 
 
function drive_space
{
    echo "<h2>Filesystem Space</h2>"
    df -h
}
# ---- END OF DRIVE SPACE ---- #
 
function backup_size
{
    echo "<h2>Backup Size</h2>"
    du -h _sqldump.sh
}
# ---- END OF BACKUP SIZE ---- #
 
function additional_function
{
    # TEMPORARY FUNCTION STUB
    echo "function additional_function"
}
# ---- END OF ADDITIONAL FUNCTIONS ---- #
 
 
# ===================================================================== #
#     GATHERS AND DISPLAYS THE STATUS OF THE SYSTEM   #
# ===================================================================== #
function ATTACHMENT
{
"cat <<-_Status_
  <html>
  <head>
  <title>$TITLE</title>
  </head>
 
  <body>
  <h1>$TITLE</h1>
  <p></p>
  $(drive_space)
  $(backup_size)
  <body>
  </html>
_Status_"> $ATTACHMENT
}
# ===================================================================== #
#    GATHERS INFORMATION AND SEND MESSAGE ATTACHMENT    #
# ===================================================================== #
 
if [ "$MAILCONTENT" = "files" ]
 then
 mutt -s " System Status $DATE" -a $ATTACHMENT -- $MAILADDR < $SUBJECTFILE
fi
 
exit 0

is your mutt supports -e then use it like below

mutt -e "my_hdr Content-Type: text/html" -s " System Status $DATE" -a $ATTACHMENT -- $MAILADDR < $SUBJECTFILE

I not get this in my e-mail, but now no attachment.

--3V7upXqbjpZ4EhLz Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, Attached is the current status of the System Kind Regards --3V7upXqbjpZ4EhLz Content-Type: text/html; charset=us-ascii Content-Disposition: attachment; filename="Status.html" --3V7upXqbjpZ4EhLz-- 

Does this mean that my mutt doesn't support -e? I did mutt --help and the -e was listed thoguh.

refer the below posts

I look at all three those posts. It seems like I'm going round in circles. I again have it attaching the HTML file, but the HTML file is empty. If I jsut run it without doing a mailer, the correct information is displayed on the screen. The problem is actually geting the information display on screen into an e-mail and then sending it to the distribution list.

# ===================================================================== #
#     GATHERS AND DISPLAYS THE STATUS OF THE SYSTEM   #
# ===================================================================== #
function ATTACHMENT
{
cat "<<-Status
  <html>
  <head>
  <title>$TITLE</title>
  </head>
 
  <body>
  <h1>$TITLE</h1>
  <p></p>
  $(drive_space)
  $(backup_size)
  <body>
  </html>
Status"> $ATTACHMENT
}
# ===================================================================== #
#    GATHERS INFORMATION AND SEND MESSAGE ATTACHMENT    #
# ===================================================================== #
 
if [ "$MAILCONTENT" = "files" ]
 then
 mutt -e "set content_type=text/html" -s "System Status $DATE" -a $ATTACHMENT -- $MAILADDR <$SUBJECTFILE
fi
 
exit 0

This:

Status"> $ATTACHMENT

Does not work. You need to redirect to a file on the first line of the here-document, for example:

cat <<-_Status_ > file
1 Like

I really have no clue what I am doing wrong. I have now changed it and it nots creating an attachment anymore. I've read all the other forums and links and even went through a complete tutorial but I really have no clue what I am doing wrong.

function ATTACHMENT
{
cat <<-_Status_ > $ATTACHMENT
 
  <html>
  <head>
  <title>$TITLE</title>
  </head>
  <body>
  <h1>$TITLE</h1>
  <p></p>
  $drive_space
  $backup_size
  <body>
  </html>
_Status_
}
# ===================================================================== #
#    GATHERS INFORMATION AND SEND MESSAGE ATTACHMENT    #
# ===================================================================== #
 
if [ "$MAILCONTENT" = "files" ]
 then
 mutt -e "set content_type=text/html" -s "OTRS System Status $DATE" -a $ATTACHMENT -- $MAILADDR <$SUBJECTFILE
fi

Error messages
Status.html: No such file or directory
Status.html: unable to attach file.

you need to call your function

 
_Status_
}
ATTACHMENT  #calling the function
1 Like

Thanks Guys, You really helped a lot. This was my first shell script that I have written and I think I have really learnt a lot. I really do appreciate the help.

---------- Post updated at 10:41 AM ---------- Previous update was at 10:37 AM ----------

Sorry guys, one more question on this. What I never had in the scripting code was the actual dump of my database.

I created a seperate function for the mysql dump, which worked perfectly. My question is though, to have the html file display the size of the backup and the space remaining there after, do I call the mysql_dump function, before creating and then calling the attachment function?