Gossamer Forum
Home : Products : DBMan : Customization :

massmailer mod with multiple databases

Quote Reply
massmailer mod with multiple databases
Hi all,

I am interested in simultaneously sending a single message to those in several databases (15 to be exact) via the massmailer mod. At times, I may desire to send a message to only to a few of the 15.

I use a single html and db.cgi file per region - each (state) database within the region has its own config file, db, and pass file.

How might I accomplish this? The massmailer mod is pretty short so I'll just post it below. Much thanks in advance.

Catster

#####################################################################
# #
# M A S S M A I L E R #
# #
# Last modified: 13 Jul 2001 #
#####################################################################

#####################################################################
# #
# This mod will allow you (as admin) to send a message to multiple #
# users in your database. #
# #
# The mod requires that you have the "sendmail" program on your #
# system. #
# #
# Note that some servers bog down if a huge number of messages are #
# sent at one time. Also, if you have a very large number of #
# messages, your browser may time out before it gets finished. If #
# this happens, send your message in batches, with more #
# restrictive search criteria for each batch. #
#####################################################################

#####################################################################
# file: default.cfg #
# #
# somewhere in the authentication definitions #
# add the following #
#####################################################################

# Full path to sendmail on your system
$mailprog = "|/usr/lib/sendmail -t -oeq";
# Your email address
$admin_email = 'you@yourserver.com';
# Fieldname that contains the email address of the user
$db_email_field = 'Email';

###############################################################################
#file: db.cgi #
# sub main #
# #
# within the other "elsif" statements #
# add the following #
###############################################################################

elsif ($in{'mass_mail_form'}) { if ($per_admin) { &html_mass_mail_form; } else { &html_unauth; } }
elsif ($in{'mass_mail'}) { if ($per_admin) { &mass_mail; } else { &html_unauth; } }

###############################################################################
#file: db.cgi #
# new subroutine #
# sub mass_mail #
###############################################################################

sub mass_mail {
# --------------------------------------------------------
#
my ($i,$message,@lines,$line,$count,%rec,$mail_count);

unless ($admin_email) { $message = "Admin email has not been entered in the .cfg file<BR>"; }
unless ($admin_email =~ /.+\@.+\..+/) { $message = "Admin email address is not in the correct format.<BR>"; }
unless ($in{'subject'}) { $message .= "You must fill in a subject for your message.<BR>"; }
unless ($in{'emailmessage'}) { $message .= "Your email message is empty.<BR>"; }
for ($i = 0; $i <= $#db_cols; $i++) {
if ($db_email_field eq $db_cols[$i]) {
$found = 1;
last;
}
}
if (!$found) {
$message .= "email field not found<BR>";
}
open (DB, "<$db_file_name") or &cgierr("error in mass_mail. unable to open database: $db_file_name.\nReason: $!");
@lines = <DB>;
close DB;
$in{'mh'} = $#lines;
my ($status, @hits) = &query("view");
unless ($status eq 'ok') {
$message .= "$status<BR>";
}
if ($message) {
chomp($message);
&html_mass_mail_form($message);
return;
}
my ($numhits) = ($#hits+1) / ($#db_cols+1);
for (0 .. $numhits - 1) {
%rec = &array_to_hash($_, @hits);
if ($rec{$db_email_field} =~ /.+\@.+\..+/) {
open (MAIL, "$mailprog") || &cgierr("unable to open mail program");
print MAIL "To: $rec{$db_email_field}\n";
print MAIL "From: $admin_email\n";
print MAIL "Subject: $in{'subject'}\n\n";
print MAIL "-" x 75 . "\n\n";
print MAIL $in{'emailmessage'};
close (MAIL);
++$mail_count;
}
}
&html_mass_mail_success($mail_count);
}


###############################################################################
#file: html.pl #
# sub html_footer #
# #
###############################################################################

print qq!| <A HREF="$db_script_link_url&mass_mail_form=1">Send Mass Mail</A> ! if ($per_admin);

###############################################################################
#file: html.pl #
# new subroutine #
# sub html_mass_mail_form #
###############################################################################

sub html_mass_mail_form {
#----------------------------------------------------------
my ($message) = $_[0];


&html_print_headers;

print qq|
<html><head><title>$html_title: Mass Mailer</title></head>
<body bgcolor="#DDDDDD">
<center>
<table border=1 bgcolor="#FFFFFF" cellpadding=5 cellspacing=3 width=500 align=center valign=top>
<tr><td colspan=2 bgcolor="navy">
<FONT FACE="MS Sans Serif, arial,helvetica" size=1 COLOR="#FFFFFF">
<b>$html_title: Mass Mailer</b></td></tr>
<tr><td>
<p><center><$font_title><b>
Mass Mailer
</b></font></center><br>
|;

if ($message) { print qq|There was a problem: $message|; }
print qq|
<$font>Fill in the subject of your message, the message you wish to send and the criteria
for the users to whom you wish to send the email.</font>
|;

print qq|
<form action="$db_script_url" method="POST">
<input type=hidden name="db" value="$db_setup">
<input type=hidden name="uid" value="$db_uid">
<table><tr><td align=right><$font>Subject:</font></td>
<td><input type=text name="subject" value="$in{'subject'}" size=40></td></tr>
<tr><td align=right><$font>Your message:</font></td>
<td><textarea name="emailmessage" cols=40 rows=10 wrap="virtual">$in{'email_message'}</TEXTAREA></td></tr>
</table>|;

&html_record_form(%in);

print qq|
<center>
<INPUT TYPE="SUBMIT" NAME="mass_mail" VALUE="Send Mail">
<INPUT TYPE="RESET" VALUE="Reset Form">
</center>
</form>
|;

&html_footer;
print qq|</td></tr></table></body></html>|;
}

###############################################################################
#file: html.pl #
# new subroutine #
# sub html_mass_mail_success #
###############################################################################

sub mass_mail_success {
#-----------------------------------------------

my ($message) = $_[0];

&html_print_headers;

print qq|
<html><head><title>$html_title: Mass Mailing Successful</title></head>
<body bgcolor="#DDDDDD">
<center>
<table border=1 bgcolor="#FFFFFF" cellpadding=5 cellspacing=3 width=500 align=center valign=top>
<tr><td colspan=2 bgcolor="navy">
<FONT FACE="MS Sans Serif, arial,helvetica" size=1 COLOR="#FFFFFF">
<b>$html_title: Send an email</b></td></tr>
<tr><td><p><center><$font_title><b>
Mass Mailing Successful
</b></font></center><br>
|;

print qq|<$font>Your email message was sent to $message users.</font>|;

&html_footer;
print qq|</td></tr></table></body></html>|;
}
Quote Reply
Re: [Catster] massmailer mod with multiple databases In reply to
It would seem that you would need to:

Make sure the field for the email address was the same for each database, then repeat the functions of the mass_mail sub for each database by defining $db_file_name each time.

I'm sure the names of the databases could be put into a database as well and read from it with a loop, but I don't know how to do that.

I would get it to work for one database, then instead of calling the variable $db_file_name I would replace it with the actual database name, then copy the whole thing and insert the second database name. Of course making sure it works along the way.