#!/usr/bin/perl -w
# ==================================================================
# DBman SQL - enhanced database management system
#
# Website : http://gossamer-threads.com/
# Support : http://gossamer-threads.com/scripts/support/
# Revision : $Id: nph-email.cgi,v 1.12 2002/02/09 00:18:12 bao Exp $
#
# Copyright (c) 2001 Gossamer Threads Inc. All Rights Reserved.
# Redistribution in part or in whole strictly prohibited. Please
# see LICENSE file for full details.
# ==================================================================
use strict;
use Dbsql qw/$DB $IN $CFG/;
use GT::Mail::BulkMail;
use vars qw(%info $extra $Is_CGI);
Dbsql::reset_env() if ($Dbsql::PERSIST);
BEGIN { local $@; eval { require Time::HiRes; import Time::HiRes qw/time/; }; }
$| = 1;
$Is_CGI = $ENV{REQUEST_METHOD};
# demo
# print $IN->header();
# die "Sending of mails has been disabled in the demo.";
local $SIG{__DIE__} = \&Dbsql::fatal;
main();
sub main {
# -------------------------------------------------------------------
# Figure out who to send email to.
#
my $ID = $Is_CGI ? $IN->param('emailsto') : shift(@ARGV);
unless ($Is_CGI or $ID) {
print "Usage: $0 mailing_id
$0 will attempt to send all the e-mails associated with that ID.\n";
exit 1;
}
unless (defined $ID and length $ID) {
Dbsql::fatal ("No Mailing ID passed to nph-email!");
}
my $mail = $DB->table('Dbsql_MailingIndex')->select({ Mailing => $ID })->fetchrow_hashref;
if (! $mail) {
Dbsql::fatal ("Invalid Mailing ID passed to nph-email!");
}
my %mail = %$mail;
my $presend = sub { GT::Template->parse('', \%info, { string => $_[1] }) };
my $sent = 0;
$extra = $DB->table('Dbsql_MailingIndex')->select({ Mailing => $ID },['done'])->fetchrow_array;
my $success = sub {
print ++$sent % 20 ? ". " : ". $sent sent
\n";
$DB->table('Dbsql_EmailMailings')->update({ 'Sent' => 1 }, { ID => shift });
};
my $faults = 0;
my $failure = sub {
$faults++;
my $mailID = shift;
my $sentstr = " . " x $sent;
print "\nThere was an error while sending the email to ";
print $DB->table('Dbsql_EmailMailings')->select({ ID => $mailID },['Email'])->fetchrow_arrayref()->[0];
$DB->table('Dbsql_EmailMailings')->update({ 'Sent' => 1 }, { ID => $mailID });
print "\n";
print $sentstr;
};
my $mailer = GT::Mail::BulkMail->new(
-show_errors => 1,
-from => $mail{mailfrom},
-name => $mail{name},
-subject => $mail{subject},
-message => $mail{message},
-success => $success,
-failure => $failure,
-text => $mail{messageformat} eq 'text',
-html => $mail{messageformat} eq 'html',
-sendmail => $CFG->{db_mail_path},
-smtp => $CFG->{db_smtp_server},
);
my $user_table = $mail->{user_table_use};
my $message = $mail{message};
my $get_cols = ['ID','Email'];
my $sth = $DB->table('Dbsql_EmailMailings')->select({ Mailing => $ID },$get_cols);
my $next = sub {
my @row = $sth->fetchrow_array;
return unless @row;
if ($user_table) {
my $info = $DB->table($user_table)->select({ Email => $row[1] })->fetchrow_hashref;
if ($info) { #get user information
my $tmp = $message;
foreach my $key (keys % $info) { $tmp =~ s/<\%$key\%>/$info->{$key}/g; }
$mailer->{message} = $tmp;
}
}
return @row;
};
my $start = time();
my $started = scalar localtime;
$IN->header(-nph => $CFG->{nph_headers});
if ($Is_CGI) {
print Dbsql::header ('Sending Emails ...','Mailing...','DBMan SQL is now going to send the emails in the associated mailing. Please be patient, this can take a while depending on the speed of the mail server and the number of recipients.','Sending Emails ...','','Sending Emails ...');
print <
Started at $started. Sending emails ... HTML } else { print <\n" : "\n"; print qq{Return to View Mailings } if $Is_CGI; }send($next); my $finished = time; $DB->table('Dbsql_MailingIndex')->update({ done => int($finished) }, { Mailing => $ID }); print "$sent sent.";# if $sent % 20; printf ("\n\nMailing complete (%.2f s)\n\n", $finished - $start); print $Is_CGI ? "