Solved

Email shell script

Posted on 2003-11-06
7
14,873 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:mre2mpo
7 Comments
 
LVL 12

Expert Comment

by:paullamhkg
ID: 9693112
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
 

Author Comment

by:mre2mpo
ID: 9693131
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
 
LVL 40

Accepted Solution

by:
jlevie earned 125 total points
ID: 9694213
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 1

Expert Comment

by:ChiefEngineer
ID: 9695240
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
 
LVL 1

Expert Comment

by:ChiefEngineer
ID: 9695275
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
 

Author Comment

by:mre2mpo
ID: 9700079
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
 
LVL 1

Expert Comment

by:ChiefEngineer
ID: 9714975
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

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question