Link to home
Start Free TrialLog in
Avatar of mre2mpo
mre2mpo

asked on

Email shell script

I need to setup a shell script with cron jobs that will e-mail a list of users from a file (I would perfer a mysql databse but might be a bit complicated!)

This is the current script that is not working - it just a test

#!/bin/sh
if `test -e mail/outmail`
then
        echo -e "From: enquiries@chickencookery.co.uk\r" >makemail
        echo -e "To:
emailaddress\r"> makemail
        mail/outmail >> makemail
        /usr/sbin/sendmail -t <makemail
        cat makemail
        rm -f makemail
fi

echo -e "/n ran script /n";

The script test to make sure that there is a file with a subject called outmail. It will then email it to the address supplied (This will be a a file) and send another e-mail me saying the script has run.

Please Help I am new to writting shell scripts

Michael
Avatar of paullamhkg
paullamhkg

How many people you going to send? if not so many, why not try the aliases function.

for eg. add entry into /etc/aliases

groups : user1, user2, ues3, user4, user5,.......

so when you send a mail to groups@yourdomain.com, all the users inside the groups will get the mail, unless you have many many users
Avatar of mre2mpo

ASKER

It is maybe quite a few it depends how many register to be sent e-mail but its not for a very busy site..
ASKER CERTIFIED SOLUTION
Avatar of jlevie
jlevie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi mre2mpo.

I'm not sure what kind of failure the script shows to you (you didn't specify it) but I guess you have a couple of little mistakes in the script code.

In first place (specially if you plan to cron this script) you have to be very carefull about relative paths (say 'mail/outmail'). If you run it from the wrong directory the script will always fail. Perhaps you should use an environment variable to specify your relative paths (i.e. '$HOME/mail/outmail' if you plan to use this for every single user).

In second place in your script you expects the 'outmail' file to be executable (you're actually running it and adding it's out to 'makemail'!). I guess that's the key to the actual error. You probably want it to be the text body of the mail, so you have to 'cat' it to the 'makemail' file instead.

In third place, I'm not actually sure about mail command taking the destination mailbox from the redirected text, but from the command line. If this way used to work to you then just forget this comment. If not you should try to put the mailbox names in the cmmand line for mail just before the input redirection (i.e. 'mail -e someone@company.com someother@othercompany.com ... <makemail').

So, I suggest you to make a few small changes to the script you posted to make it work and more flexible for manteinance. Those changes are like follows:
   a) Use environment variables for both the base file paths and the e-mail addresses so you can easly manage them.
       I added the variables BASEDIR for the location of the 'mail/outmail' file and EMAILADDRESS for the detination of the
       mail. This second variable will be of great help when you turn this into a list of mailboxes (as you stated you need to).
       I also initialized the BASEDIR variable to '.' (the actual dir) to keep the script actual behavior, but you can easly change
       this.
   b) 'cat' the content of the file 'mail/outmail' instead of running it.
   c) Also change the escape slashes you use in the final message from '/n' to '\n' so it will really send a new line ;-)

The script should look like this then:

#!/bin/sh

BASEDIR=.
EMAILADDRESS=<put_the_destination_here>

if `test -e $BASEDIR/mail/outmail`
then
        echo -e "From: enquiries@chickencookery.co.uk\r" >makemail
        echo -e "To: $EMAILADDRESS\r"> makemail
        cat mail/outmail >> makemail
        /usr/sbin/sendmail -t <makemail
        cat makemail
        rm -f makemail
fi

echo -e "\n ran script \n"

If you want to add the list of destinations functionality, you just need to make a few changes like this:

#!/bin/sh

BASEDIR=.
MAILBOXLIST=$BASEDIR/mail/mailbox.list

if `test -e $BASEDIR/mail/outmail`
then
    for EMAILADDRESS in `cat $MAILBOXLIST`
    do
        echo -e "From: enquiries@chickencookery.co.uk\r" >makemail
        echo -e "To: $EMAILADDRESS\r"> makemail
        cat mail/outmail >> makemail
        /usr/sbin/sendmail -t <makemail
        cat makemail
        rm -f makemail
    done
fi

echo -e "\n ran script \n"

You just have to change the value of the variable MAILBOXLIST to whatever you decide your destinations list file to be, and that file must have only a text list of every mailbos (one per line).

Hope it helps...
Hey!

I was reading my recent answer and then realized the real problem.

You are using 'sendmail', which is the daemon for mail delivery and not the actual mail client.

You should replace the command '/usr/sbin/sendmail' with the command 'mail' which is the mail client program and is the usual way to send mail from scripts.

Check 'man mail' for mail command line options (like recipients, subject, etc.).
Avatar of mre2mpo

ASKER

Thank you for superb help I now have the script working all I need to do is add users to the list when they enter their e-mail address from the web page I should be able to do this so thanks agian

Michael
We're glad to be helpful.
If an Expert helps you, please accept his/her answer with an excellent or good grade.
(thanks to war1 for this words. See https://www.experts-exchange.com/questions/20784156/Automatic-Updates-XP-Same-update-keeps-downloading.html#9699023)
Thanks.