Solved

Email shell script

Posted on 2003-11-06
7
14,866 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 1

Expert Comment

by:ChiefEngineer
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Mac - rsync folders with / in name 5 59
Problem logging tar errors 11 35
LINUX, CPANEL & WHM 5 18
BASH script to modify crontab? 3 19
This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now