#!/usr/local/bin/perl # This script sends out an email to a specified group of individuals from # the database use CGI qw(:all); use DBI; use Net::SMTP; use MIME::Lite; print header; #################### # PROGRAM OVERVIEW # #################### # Is user admin? If not kill process. isadmin(); # Display an email form. # - Subject # - Content # - Send # Clicking the send button will # check if all email fields are filled out. # And if they are, will process the data, and send the # email to the selected users. # If not, it will display the form again. if (param("subject") and param("body")) { # Get the array / hash of NL subscribers getsubscribers(); sendemail(); # Show the results of the routine. showresults (); } else { displayform(); } ######################## # SUBROUTINES ########## ######################## sub isadmin { $servername = $ENV{'SERVER_NAME'}; ## Sends Environement Variables if ($servername eq "localhost" or $servername eq "JaredWork"){ $remoteuser = 'admin'; } elsif ($servername eq "www.wilcoxassoc.com"){ $remoteuser = $ENV{'REMOTE_USER'}; } else{ foreach $key (sort(keys %ENV)){ print ("
$key = $ENV{$key}"); } &dienice("Couldn't connect to unknown servername of: $servername"); } $remoteuser = lc($remoteuser); $datasource = 'dbi:mysql:userinfo'; $dbusername = 'perlscript'; $dbpassword = 'scriptpass'; ## Connect to database $dbh = DBI->connect($datasource,$dbusername,$dbpassword) or dienice ("Can't connect: $! ." . $DBI::errstr ); ## Grab user info from the table $sth = $dbh->prepare("SELECT * FROM registered WHERE username = ?") or dienice ("Couldn't prepare select statement: $!" . $dbh->errstr); $sth->execute ($remoteuser) or dienice ("Couldn't execute prepared statement $!" . $dbh->errstr); @row = $sth->fetchrow_array(); $user = $row[0]; # Get their username $name = $row[1]; # Get their name $email = $row[2]; # Get their email $isadmin = $row[4]; # Get the admin column if (length($name)<=0) { dienice("There is no username that matches $remoteuser in the database. Use a different username."); } if ($isadmin ne "Y" and $isadmin ne "S") { dienice("$name, You do not have priveleges to send out a mailing to the users."); } } sub getsubscribers { $sth = $dbh->prepare("SELECT * FROM subscriber") or dienice ("Couldn't prepare select statement to get list of NL subscribers: $!" . $dbh->errstr); $sth->execute() or dienice ("Couldn't execute prepared statement $!" . $dbh->errstr); while (@row = $sth->fetchrow_array) { $name = $row[2]; # Get their names $email = $row[1]; # Get their emails $subkey = $row[7]; # Get their subkeys # print "
Testing... $name, $email, $subkey"; push @names,$name; push @emails,$email; push @subkeys,$subkey; } } sub displayform { print "
Use this form to send out emails to newsletter subscribers. Simply type the subject of the email and the body into the form below. You don't need to include a 'Dear (name)' line. This is handled automatically:
"; # print "Testing...list of emails to send to: @emails
"; $locscriptdownload='../scriptdownload.pl'; $accountmanagement='accountmanagement.pl'; print <Return to the Account Management Area
FORM } sub sendemail { print "This email message will be sent:"; $body = param("body"); $subject = param("subject"); print "
Subject: $subject"; print "
Body: $body
...Attempting to send message to user:"; print "
Dear $name,
$body
To unsubscribe from the WAI Newsletter, follow this link: $locunsubscribeCOULD NOT SEND TO the following emails:";
print "
@notsent
All emails were sent."; } print "
Return to the Account Management Area
"; print ""; } #################### ### DYING NICELY ### sub dienice { my($errmsg) = @_; print <$errmsg