sub sendemail { $j = 0; foreach $i(@names){ ## Grab user info from the table $name = $names[$j]; # Get their name $email = $emails[$j]; # Get their email $subkey = $subkeys[$j]; #Get their subscriber key $remotehost = $ENV{'HTTP_HOST'}; $remotehost = "http://" . $remotehost . "/"; $locunsubscribe = 'perl/nl_unsubscribe.pl' . '?' . $subkey; $locunsubscribe = $remotehost . $locunsubscribe; mailit(); $j++; } } sub mailit{ print "
...Attempting to send message to user:"; print "
Dear $name,
$body
To unsubscribe from the WAI Newsletter, follow this link: $locunsubscribe##
# Show the results of the send. This sub checks to
# see if the array contains any addresses that it coulnd't send to and displays them:
sub showresults{
print "Showing Results
";
if (@notsent) {
print "Out of the following emails:";
print "
@to";
print "
The following were not sent:";
print "
@notsent
";
}
else {
print "Emails successfully sent to these addresses:";
print "
@to
";
}
print "";
print "";
}
####
$msg->send or push @notsent,$email;
####
#!/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 "
PC-DMIS Newsletter Subscribers - Email Form
";
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 "Entering Send Mail Routine
";
print "This email message will be sent:";
$body = param("body");
$subject = param("subject");
print "
Subject: $subject";
print "
Body: $body
";
$j = 0;
foreach $i(@names){
## Grab user info from the table
$name = $names[$j]; # Get their name
$email = $emails[$j]; # Get their email
$subkey = $subkeys[$j]; #Get their subscriber key
$remotehost = $ENV{'HTTP_HOST'};
$remotehost = "http://" . $remotehost . "/";
$locunsubscribe = 'perl/nl_unsubscribe.pl' . '?' . $subkey;
$locunsubscribe = $remotehost . $locunsubscribe;
mailit();
$j++;
}
}
sub mailit{
print "...Attempting to send message to user:";
print "
Record for $user
";
print "";
print "- User: $user";
print "
- Name: $name";
print "
- Email: $email";
print "
";
print "
";
my $wai = 'Wilcox Associates Inc.';
my $when = localtime();
my $locwai = '../Contactinfo/feedback.htm';
my $mail_host = 'mail.wilcoxassoc.com';
# Replace w/ browse button on form to get these.
my $fullpath = 'd:\temp\test.pdf';
my $filename = 'test.pdf';
### Create the multipart container
$msg = MIME::Lite->new (
From => $wai,
To => $email,
Subject => $subject,
Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";
### Add the text message part
$msg->attach (
Type => 'TEXT/HTML',
Data => "Dear $name,
$body
To unsubscribe from the WAI Newsletter, follow this link:
$locunsubscribe
THIS IS AN AUTOMATED MESSAGE! PLEASE DON'T REPLY TO THIS EMAIL."
) or die "Error adding the text message part: $!\n";
### Add the specified PDF file
$msg->attach (
Type => 'application/pdf',
Path => $fullpath,
Filename => $filename,
Disposition => 'attachment'
) or die "Error adding $fullpath: $!\n";
### Send the Message
MIME::Lite->send('smtp', $mail_host, Timeout=>60);
$msg->send or push @notsent,$email;
}
# Show the results of the send. This sub checks to
# see if the array contains any addresses that it coulnd't send to and displays them:
sub showresults{
print "Showing Results
";
if (@notsent) {
print "COULD NOT SEND TO the following emails:";
print "
@notsent
";
}
else {
print "All emails were sent.";
}
print "
Return to the Account Management Area
";
print "";
}
####################
### DYING NICELY ###
sub dienice {
my($errmsg) = @_;
print <ERROR!
ERROR...
$errmsg