use shell to send email automatically

Hi All
    i have one text file a.log at /usr/local/nagios/var , i would like to use Shell to write script and send the a.log file as attachment to me email everyday, how to write the script and how to set the regular sending? my server is linux centos6.5
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zerox HoopLinux AdminCommented:

you can use heirloom-mailx  & mailx for send attachment email.
First you need to install mailx in your machine.

# yum install -y mailx
Set an External SMTP Server to Relay E-Mails
# vi /etc/mail.rc and edit

set smtp=smtp://
# tell mailx that it needs to authorise
set smtp-auth=login
# set the user for SMTP
# set smtp-auth-user=user@domain.tld
# set the password for authorisation
set smtp-auth-password=enter-password-here-1234

save and exit
Here are some of the available options :

    * -a file Allows you to attach the given file to the e-mail
    * -b address Sends blind carbon copies to the comma separated e-mail address list
    * -c address Sends copies to a list of users
    * -q file Sets the message contents from the given file
    * -r from address Sets the from address of the e-mail to be sent
    * -s subject Sets the e-mail subject

Sending a message with an attachment:
echo "Message" | mail -s "Subject" -a /loc/to/attachment.txt email@address

if you need it in full details then please see below link.
beardog1113Author Commented:
thank you, i just add new job in crontab, "1 * * * * /usr/local/nagios/var/", also i created, "sendmail < /usr/local/nagios/var/nagios.log
", but it was not working, how should i write the scirpt?
Dave GouldOnsite SupportCommented:
change it to:
mailx -s "Daily logs" < /usr/local/nagios/var/nagios.log

You also said that you wanted the log as an attachment so you could try this:

cd /usr/local/nagios/var
uuencode nagios.log nagios.log | mailx
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

beardog1113Author Commented:
thank you, now the is working i can run this script succesfully, and i can get the email. but after i ask crontab to run this, it is not working, the crontab setting is "1 * * * * /usr/local/nagios/var/", what can i do?
Zerox HoopLinux AdminCommented:
mailx -s "Subject" -a /usr/local/nagios/var/nagios.log

and 2nd is

heirloom-mailx -s "Log:Details" < /usr/local/nagios/var/nagios.log
Dave GouldOnsite SupportCommented:
So you have asked for it to run 1 minute after every hour. Have you waited long enough?
Whose crontab is it in? Does that user have mail mailx in his path (should install in /bin )?

If in doubt, add a log file to you crontab (you should do this anyway to avoid unwanted mails).

1 * * * * /usr/local/nagios/var/ > /tmp/sendmaildaily.log 2>&1
beardog1113Author Commented:
# crontab -u root -l is "1 * * * *  /usr/local/nagios/var/ /tmp/sendmaildaily.log 2>&1"

#vim /usr/local/nagios/var/  is "#!/bin/sh   sendmail  /usr/local/nagios/var/nagios.log"

i van run the manually, but autorun is not working
Zerox HoopLinux AdminCommented:
do simple step..

#crontab -e
For one minute
*/1 * * * * sh /usr/local/nagios/var/
Note: give executable permission to script Like: chmod +x /usr/local/nagios/var/
that's it

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave GouldOnsite SupportCommented:
You should have a ">" between the and /tmp/......
Once you've run the script via crontab, the /tmp/..... file will probably give some indication as to why the job is not running.
A presume that the crond deamon is running ?
beardog1113Author Commented:
the log file has been created now, the error is "/usr/local/nagios/var/ line 2: sendmail: command not found", for crontab setting, "*/1 * * * * sh /usr/local/nagios/var/", i have not write "sh" after "* * * *", i think that is why the log file has not been created before. now how to fix sendmail command not found? thank you alll
beardog1113Author Commented:
Hi All
    after i use mailx -s "Subject" -a /usr/local/nagios/var/nagios.log instead of sendmail command, it is working now
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.