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, |
Use this SendEmail command line "script" for sending e-mails with HTML content or attachments
Quote:
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" Good luck. |
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. |
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:
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 |
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... |
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. |
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 Code:
$ incrond -h <--- yes, it's "incrond" - deamon service 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 Make a log file to make it available to generate reports into: Code:
touch /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 put the code that follows into "incrontab", the directory I will make "/usr/tmp/inotifytestdir" to test it: Code:
$ incrontab -e It returns nothing and you can check it running: Code:
$ ps aux | grep incron check the log file and your email: Code:
$ cat /var/log/inotify_changes_test.log |
Thanks a lot for replying ...I will post after doing this.
|
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 |
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 11:39 PM. |