Send email with attachment from linux command line using UUENCODE

After reading this you can able to send mails with attachments from linux command prompt :)

Let’s start,

Scenario 1:

You need to send a email from the linux command prompt with the log file attached.

The log file name is myprogram.log and located in /var/log/myprogram.log

The log need to be send to someone@mycompany.com

Make sure your linux box having the “uuencode” tool installed. Also you need a “Mail Transport Agent”. in this example am using “mail” (like mutt,mailx)

Step 1:

Am creating a file automail.sh

Command:

vi automail.sh

Step 2:

Am adding the following script to the newly opened file

#!/bin/bash

MYFILE=/var/log/myprogram.log

(echo “Please find the attached log”; uuencode $MYFILE myprogram.log;) | mail -s “My Program log” someone@mycompany.com

(above script highlighted in red is a full single line)

Step 3:

Save the file and exit from vi editor

Step 4:

Change the permission of the file to executable

Command:

chmod 700 ./automail.sh (assuming the file in current directory and the login use is root)

That’s it. This script will send the log file attached in the email with subject “My Program log” and the body of the message “Please find the attached log”

Scenario 2:

You need to send a log file emailed to your boss every day from the linux command prompt.

The log file name is not same every day. The log file name stored in the following format myprogram_2008_03_28.log, next day myprogram_2008_03_29.log and so

on.

In this example, we are sending the log in the same day when it generated.

Let’s start,

Step 1:

Am creating a file automail-random.sh

Command:

vi automail-random.sh

Step 2:

Am adding the following script to the newly opened file

#!/bin/bash

NDATE=$(date +%Y-%m-%d)

MYFILE=/var/log/myprogram_$NDATE.log

(echo “Please find the attached log”; uuencode $MYFILE myprogram_$NDATE.log;) | mail -s “My Program log” someone@mycompany.com

(above script highlighted in red is a full single line)

Step 3:

Save the file and exit from vi editor

Step 4:

Change the permission of the file to executable

Command:

chmod 700 ./automail-random.sh (assuming the file in current directory and the login use is root)

That’s it. This script will send the log file attached in the email everyday with subject “My Program log” and the body of the message “Please find the attached log”

Wait a minute.

What happen if the file does not exist? No NUKE. The program simply fails to execute.

To avoid this, add the “if” condition to the program and validating the file can overcome the trouble.

The final program with the “if” condition below.

#!/bin/bash

NDATE=$(date +%Y-%m-%d)

MYFILE=/var/log/myprogram_$NDATE.log

if [ -f $MYFILE ]; then

(echo “Please find the attached log”; uuencode $MYFILE myprogram_$NDATE.log;) | mail -s “My Program log” someone@mycompany.com

(above script highlighted in red is a full single line)

else

mails -s “Log File is not Generated or Not found” someone@mycompany.com < /dev/null

fi

#—–END—–#

HAVE FUN….



Leave a Reply