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
mre2mpoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

paullamhkgCommented:
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
0
mre2mpoAuthor Commented:
It is maybe quite a few it depends how many register to be sent e-mail but its not for a very busy site..
0
jlevieCommented:
It's easy enough to do this from a user list stored a database with Perl. I happen to prefer Postgres over MySQL, but the techniques are the same as in this code:

#!/usr/bin/perl
#
# For each entry in the "mass-mail.txt" file send the message specified on
# the command line. The message should have From:, Subject:, and Reply-To:
# lines as appropiate.
#
if($#ARGV !=0) { die "No message specified\n"; }

$query  = "SELECT email FROM accounts WHERE newsletter='y'";
#
# Prepare the query, then execute it
#
$st = $db->prepare($query)
  || die $DBI->errstr;
$res = $st->execute() || die $DBI->errstr;
while(($email) = $st->fetchrow_array)
{
    print STDOUT "$email\n";
    $cmd = "/usr/sbin/sendmail $email <$ARGV[0]";
    system $cmd;
    sleep 1;
}

To do that with the addresses in a text file the code might look like:

#!/usr/bin/perl
#
# For each entry in the address list file send the message specified on
# the command line. The message should have From:, Subject:, and Reply-To:
# lines as appropiate.
#
if($#ARGV !=1)
{
  die "usage: mass-mail address-file message-file\n";
}
open(ADDR, $ARGV[0]) or die "Can't open address list\n";
while(<ADDR>)
{
  chop;
  print STDOUT "$_\n";
  $cmd = "/usr/sbin/sendmail $_ <$ARGV[1]";
  system $cmd;
  sleep 1;
}
0

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
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!

ChiefEngineerCommented:
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...
0
ChiefEngineerCommented:
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.).
0
mre2mpoAuthor Commented:
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
0
ChiefEngineerCommented:
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 http://www.experts-exchange.com/Operating_Systems/Q_20784156.html#9699023)
Thanks.
0
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
Linux

From novice to tech pro — start learning today.