reading a file and sending mail by shell scripting..?? Plz help urgent..!!
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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 .
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.
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
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.
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):
$ 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.
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..??
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 ----------------
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.