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
Solved

Setting a allusers email

Posted on 2000-02-28
11
240 Views
Last Modified: 2013-12-16
Hi There
I need to setup a email that will email every user on the system
has anyone got any ideas how to do it

Phil
0
Comment
Question by:will_do
  • 6
  • 5
11 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 2566894
Well, you could create an alias that included all user names, but that gets very unwieldy for a large number of users. You could also create a list of users and use a shell script to read the list and send the emails, but then you need to keep the list up to date. The last way, which I use, is to use a perl script to walk the password file, skipping system and a few other accounts and mailing a message to all others.

I can't get to the perl code from where I am now, but I'll get it and paste it into a comment later this evening.
0
 

Author Comment

by:will_do
ID: 2566905
Thankyou
0
 
LVL 40

Accepted Solution

by:
jlevie earned 50 total points
ID: 2570823
Here tis...

#!/usr/bin/perl
#
# NAME
#       mailer - all user emailer
#
# SYNOPSIS
#       mailer from subj msg
#
# DESCRIPTION
#
#       Mailer sends the data in the "msg" file to each user account listed in
#       the system's password file (except for those in the "skip" list
#       below). The "From:" and "Subject:" header lines are built with the
#       data contained in the "from" and "subj" arguments, or sent inline if the
#       mail program allows.
#
#       On on older Solaris or other systems that don't have a readily avalable
#       mailer that accepts in "Subject" and "From" args, a temp file is built in
#        /tmp/mailer-temp that contains the header lines and message. This
#       is controlled by the "$usetmp" variable below.
#
#       Note: systems other than Linux or Solaris may require a tweak to
#       the "system" line that mails the message.
#
#       Author: James K. Levie
#

#
# Whether or not to use a temp file.
#
$usetmp = 0;
#
# A list of user accounts to skip.
#
@skip = ("root", "bin", "daemon", "adm", "lp", "sync", "shutdown",
         "halt", "mail", "news", "uucp", "operator", "games", "gopher",
         "ftp", "nobody", "xfs");

if($#ARGV!=2)
{
  die "Useage: mailer from subject message-file\n";
}

if($usetmp)
{
  $TMPF = "/tmp/mailer-temp";
  open(MSG, "$ARGV[2]") || die "Can't open message file <$ARGV[2]>\n";
  open(TMP, ">$TMPF") || "Can't create temp file <$TMPF>\n";
  #
  # Put the headers into the tmp file
  #
  print TMP "From: $ARGV[0]\n";
  print TMP "Subject: $ARGV[1]\n\n";
  #
  # Copy the message into the temp file
  #
  while(<MSG>) { print TMP; }
  close(MSG);
  close(TMP);
}
#
# Walk the passwd file
#
while(($name) = getpwent)
{
  $sendit = 1;
  #
  # Check the list of system accounts and other accounts
  # that shouldn't get the email against this username.
  #
  foreach $skipit (@skip)
  {
    if(($name=~/$skipit/)) { $sendit = 0; last:}
  }
  if($sendit)
  {
    #
    # Okay mail it.
    #
    if($usetmp)
    {
      system "mail -w $name <$TMPF";
    }
    else
    {
      system "mail -s \"$ARGV[1]\" $name <$ARGV[2]";
    }
    #
    # Brag about it...
    #
    print "Sent to $name\n";
  }
}
if($usetmp) { unlink($TMPF); }
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:will_do
ID: 2572103
thankyou
but how do i set it up
where do i put the script?
do i add a allusers account?
sorry i'm very new to linux
0
 

Author Comment

by:will_do
ID: 2572142
thankyou
but how do i set it up
where do i put the script?
do i add a allusers account?
sorry i'm very new to linux
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2573148
Okay, open a new file named "mailer" and paste the perl code into it. Close the file and make it executable with "chmod +x mailer". If you only want to use it from one account, just leave "mailer" in that user's home dir, otherwise you could copy the file to someplace that multiple users can get to, like /usr/local/bin or /usr/bin.

To use it, create a file containing the message and invoke the utility like "mailer sender "The Subject" message-file".
0
 

Author Comment

by:will_do
ID: 2573998
Thanks jlevie once again
but i just get mailer command not found when i type in mailer sender

0
 
LVL 40

Expert Comment

by:jlevie
ID: 2574060
If you are in the directory that you've created the mailer script file in, it might be that the current directory isn't in your path. Try "./mailer sender "A subject" msg-file". If that fails, make sure that it's executable:

levie> ls -l mailer
-rwxrwxr-x   1 levie    staff        2211 Feb 29 17:09 mailer*

The "x"s above indicate executable mode for user, group, and other.
0
 

Author Comment

by:will_do
ID: 2574152
ok i think i have got it

but this comes up
[root@phil allusers]# mailer sender "The Subject" message"




and the > just keep coming until you use ctrl c

0
 
LVL 40

Expert Comment

by:jlevie
ID: 2574181
There's an extra " at the end of the line after message. There have to "s around the second argument as a subject will usually contain spaces.
0
 

Author Comment

by:will_do
ID: 2574208
cool thankyou i got it

0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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 …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

809 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