Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Email shell script

Posted on 2003-11-06
7
Medium Priority
?
14,895 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

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 …
How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

715 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