LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   reading a file and sending mail by shell scripting..?? Plz help urgent..!! (http://www.linuxquestions.org/questions/linux-newbie-8/reading-a-file-and-sending-mail-by-shell-scripting-plz-help-urgent-903329/)

sukhdip 09-16-2011 05:05 AM

reading a file and sending mail by shell scripting..?? Plz help urgent..!!
 
Hi!

I need help from u guys. I wanna make a script that read a text file from a directory and send a mail to my id.
I also wanna attach that file with the mail using script only. or I can send that file content as html text in mail.

Please help me out. Its kinda urgent.

Thanks and Regards,

lithos 09-16-2011 05:16 AM

Use this SendEmail command line "script" for sending e-mails with HTML content or attachments

Quote:

Synopsis: sendEmail -f ADDRESS [options]

Required:
-f ADDRESS from (sender) email address
* At least one recipient required via -t, -cc, or -bcc
* Message body required via -m, STDIN, or -o message-file=FILE

Common:
-t ADDRESS [ADDR ...] to email address(es)
-u SUBJECT message subject
-m MESSAGE message body
-s SERVER[:PORT] smtp mail relay, default is localhost:25

Optional:
-a FILE [FILE ...] file attachment(s)
-cc ADDRESS [ADDR ...] cc email address(es)
-bcc ADDRESS [ADDR ...] bcc email address(es)
-xu USERNAME username for SMTP authentication
-xp PASSWORD password for SMTP authentication

Paranormal:
-b BINDADDR[:PORT] local host bind address
-l LOGFILE log to the specified file
-v verbosity, use multiple times for greater effect
-q be quiet (i.e. no STDOUT output)
-o NAME=VALUE advanced options, for details try: --help misc
-o message-content-type=<auto|text|html>
-o message-file=FILE -o message-format=raw
-o message-header=HEADER -o message-charset=CHARSET
-o reply-to=ADDRESS -o timeout=SECONDS
-o username=USERNAME -o password=PASSWORD
-o tls=<auto|yes|no> -o fqdn=FQDN


Help:
--help the helpful overview you're reading now
--help addressing explain addressing and related options
--help message explain message body input and related options
--help networking explain -s, -b, etc
--help output explain logging and other output options
--help misc explain -o options, TLS, SMTP auth, and more

For example if you want to send a content of the file "myfile.txt" in HTML form and also attach the same file as attachment use:
Code:

$sendEmail -f "from-sender@address.net" -t "recipient_email@example.com" -u "message Subject" -o message-content-type=html -o message-file="myfile.txt"  -a "myfile.txt"
Sep 16 1:2:3 xxxx sendEmail[123456]: Email was sent successfully!

So that's it, a message (email) with HTML format and also an attachment .

Good luck.

sukhdip 09-16-2011 06:08 AM

thanks a lot for your reply.
Actually i'm new to shell scripting part. 3 days only have read basics and all.
Can you please give me a full scenario or the links that how and which script will read the file and send mail to respective.

I'm confused.

lithos 09-16-2011 09:10 AM

Huh, I didn't understand well ;)

but let's go through the steps:
1. you need a script in the command line shell (presume BASH is your shell)
example of a shell script would be (let's name it: "myscript.sh") :
Quote:

#!/bin/bash
# declare STRING variable
STRING="Hello World"
#print variable on a screen
echo $STRING
2. you need a file to get read and presumably you know the file_name (let's say "myfile.txt")
so all you need to know is where the file is (let's say again it's in the /var/tmp/myfile.txt)
3. at last you need to 'call' the sendEmail script to send the file to your email

So the whole script would be:
Code:

#!/bin/bash
# declare the file you want to read
MYFILE=/var/tmp/myfile.txt
# recipients email address
RECP=your_email@example.com
# sender's email address
SENDER=sender@example.com
#send the contents as HTML and also attachment
sendEmail -f $SENDER -t RECP -u "message Subject" -o message-content-type=html -o message-file=$MYFILE  -a $MYFILE

and this should do it.

sukhdip 09-19-2011 02:28 AM

Hi! May be I wasn't able clear out the scene.
Is it like this:
Suppose I have a folder called logs. whenever some error occurs some correspondence error file is generated.
I need a script that can first find out that there is a file with the string error in it. then can read it or can directly attach it with the mail(or in html text) and can send a mail to respected mail. can you please give me whole scene about it. as m confused with scripting but i have to do it.

Thanks, waiting...

lithos 09-19-2011 03:00 AM

Huh, I don't know the exact commands, but
linux-inotify (triggering-commands-on-file-or-directory-changes-with-incron)
seems a good choice for this kind of job you need.

I hope someone has more experience to help you with this.

P.S: I'll try to investigate it if I have some more spare time.

lithos 09-19-2011 03:52 PM

Well,
it seems that this "inotify/incron" will do it very nicely.

As I don't know which OS you're running on the machine, I'll presume RHEL5 (which is supported very well)
so the steps you need to make (including the sendEmail in the post above):

1. install the INCRON with
Code:

$ yum install incron
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package incron.i386 0:0.5.9-2.el5.rf set to be updated
--> Finished Dependency Resolution
Downloading Packages:
incron-0.5.9-2.el5.rf.i386.rpm                                                                              | 328 kB    00:00   
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing    :  incron                                                                            1/1

Installed:
  incron.i386 0:0.5.9-2.el5.rf                                                                                                     
Complete!

Check if you can run it:
Code:

$ incrond -h            <--- yes, it's "incrond" - deamon service
incrond - inotify cron daemon
(c) Lukas Jelinek, 2006, 2007, 2008

usage: incrond [<options>]

<operation> may be one of the following:
These options may be used:
  -?, --about                  gives short information about program
  -h, --help                  prints this help text
  -n, --foreground            runs on foreground (no daemonizing)
  -k, --kill                  terminates running instance of incrond
  -f <FILE>, --config=<FILE>  overrides default configuration file  (requires root privileges)
  -V, --version                prints program version

For reporting bugs please use http://bts.aiken.cz

#>incrond -V
incrond 0.5.9

2. make a script that will run whenever a file is created in the directory you want to monitor
I will make the script where the $PATH is reachable -> /usr/local/bin
I will name it inotify_script.sh
you can use your favorite editor, I use "$ vi /usr/local/bin/inotify_script.sh"
Code:

#!/bin/bash
logfile=/var/log/inotify_changes_test.log
path=$1
file=$2
event=$3
# use date format according to your wishes how you want the time and date displayed
# i.e. you can use: datetime=$(date +"%F %T"), I use simply "date"
datetime=$(date)
echo "${datetime} Change made in path: " ${path} " Event: " ${event} >> ${logfile}
echo "${datetime} Change made to file: " ${file} >> ${logfile}
echo "${datetime} ------------------- End ----------------" >> ${logfile}
MYFILE=${path}/${file}
# recipients email address
# define the address to receive the email
RECP=your_email@example.com
# sender's email address
SENDER=sender@example.com
#send the contents as HTML and also attachment
# echo "sendEmail -f $SENDER -t $RECP -u "message Subject" -o message-content-type=html -o message-file=$MYFILE  -a $MYFILE" >> ${logfile}
# sendEmail -f $SENDER -t $RECP -u "File created - $file" -o message-content-type=html -o message-file=$MYFILE  -a $MYFILE
sendEmail -f $SENDER -t $RECP -u "File created - $file"  -m "File \"$file\" was created at $datetime. "  -a $MYFILE

Save it and exit, and make the script executable with: "$ chmod 740 /usr/local/bin/inotify_script.sh"
Make a log file to make it available to generate reports into:
Code:

touch /var/log/inotify_changes_test.log
It is empty now, check "$ cat /var/log/inotify_changes_test.log"
It should return back the prompt, nothing printed.

3. decide which directory you want to monitor to let the "incron" run
currently there's nothing defined yet, check:
Code:

$ incrontab -l
no table for root

so let's define what directory to monitor and run the "inotify_script.sh" to e-mail you:
put the code that follows into "incrontab", the directory I will make "/usr/tmp/inotifytestdir" to test it:
Code:

$ incrontab -e
/usr/tmp/inotifytestdir IN_CREATE /usr/local/bin/inotify_script.sh $@ $# $%


- save & exit the editing

Now run it: $ incrond
It returns nothing and you can check it running:
Code:

$ ps aux | grep incron
xtxxxxs    21519  0.0  0.1  3064  936 ?        Ss  22:33  0:00 incrond

now make a new file in the directory (or simply copy one there) and you will get "report" in the log and emailed the file!
check the log file and your email:
Code:

$ cat /var/log/inotify_changes_test.log
Mon 19 Sep 2011 10:36:14 CET Change made in path:  /usr/tmp/inotifytestdir  Event:  IN_CREATE
Mon 19 Sep 2011 10:36:14 CET Change made to file:  README
Mon 19 Sep 2011 10:36:14 CET ------------------- End ----------------

you can see the file "README" was created and emailed to your address.

sukhdip 09-19-2011 10:13 PM

Thanks a lot for replying ...I will post after doing this.

sukhdip 09-21-2011 05:41 AM

Thanks a lot its working...
Can you please help me in one more step that the script read the content of the file as well and put then into some file like logs file its genereating....
can this be done..??

lithos 09-21-2011 08:55 AM

into what file would you like to copy the contents ? Is it known what filename/ where it is ?
wouldn't it be easyer if you just copy that log file to some other directory or something (instead of reading the contents and copying)?

yes it can be done, but please be specific about it.

It can be done to copy the contents into the "/var/log/inotify_changes_test.log" file, but the 'source' file must be in text format, else the consequences can be unpredictable
the file "/var/log/inotify_changes_test.log" can look like this:
Code:

Mon 19 Sep 2011 10:36:14 CET Change made in path:  /usr/tmp/inotifytestdir  Event:  IN_CREATE
Mon 19 Sep 2011 10:36:14 CET Change made to file:  README
Mon 19 Sep 2011 10:36:14 CET ------------ File contents -------------
#!/bin/bash
logfile=/var/log/inotify_changes_test.log
path=$1
file=$2
event=$3
# use date format according to your wishes how you want the time and date displayed
# i.e. you can use: datetime=$(date +"%F %T"), I use simply "date"
datetime=$(date)
echo "${datetime} Change made in path: " ${path} " Event: " ${event} >> ${logfile}
echo "${datetime} Change made to file: " ${file} >> ${logfile}
echo "${datetime} ------------------- End ----------------" >> ${logfile}
MYFILE=${path}/${file}
# recipients email address
# define the address to receive the email
RECP=your_email@example.com
# sender's email address
SENDER=sender@example.com
#send the contents as HTML and also attachment
# echo "sendEmail -f $SENDER -t $RECP -u "message Subject" -o message-content-type=html -o message-file=$MYFILE  -a $MYFILE" >> ${logfile}
# sendEmail -f $SENDER -t $RECP -u "File created - $file" -o message-content-type=html -o message-file=$MYFILE  -a $MYFILE
sendEmail -f $SENDER -t $RECP -u "File created - $file"  -m "File \"$file\" was created at $datetime. "  -a $MYFILE
Mon 19 Sep 2011 10:36:14 CET ------------------- End ----------------


sukhdip 10-03-2011 02:16 AM

Hi! I'm replying after many days...Was stuck into something..
Hey thanks for your help .. My problem scenario is like below:-

1.I am doing some activity. And in this some error occurs.
2. This will generates a log file in logs folder say,:/logs/somename_ERROR_2011.10.03_11.36.30.363.log
3. the above INCRON,explained by you, will see the logs folder with some changes in it and will report the changes into some file as per explained by you above.
4.Also I wanna read this file i.e., somename_ERROR_2011.10.03_11.36.30.363.log, and wanna extracts some lines form this file and to send it to somemail address. with the content of file.
5. OR if its not possible to extracts some lines from the file then the whole file is should send to mail as HTML/TXT/ or attaching file itself.

This is the case..Hope I made it clear to you...thanks a lot


All times are GMT -5. The time now is 12:44 PM.