#!/usr/bin/perl -Tw
# ensure all fatals go to browser during debugging and setup
# *don't* uncomment these lines on production code for security
# BEGIN {
# $|=1;
# print "Content-type: text/html\n\n";
# use CGI::Carp('fatalsToBrowser');}
use strict;
use Fcntl (':flock');
my $mime = (eval 'use MIME::Entity') ? '' : 1; # check if MIME::entity installed, required to send HTML and Binaries
use CGI;
my $q = new CGI; # create new CGI object
$CGI::DISABLE_UPLOADS = 0; # enable uploads
$CGI::POST_MAX = 1048576; # limit the maximum upload size to 1MB
# we declare an Unindent prototype here which we use for our herepages
sub Unindent;
# to enable mail queueing for sendmail set this to '-odq' (set to '' for no queue)
# *warning* -odq option needs to be configured or messages may never be delivered!
my $odq = '';
.....
sub Mailout {
(my $date = $datetime) =~ s/\s\d\d:\d\d\:\d\d//;
my $recordfile = $file;
my @archive = &GetRecords('archive.txt');
$recordfile = $file." :re-send" if grep{/$file/}@archive;
my @subscriber = &GetRecords('subscriber.txt');
my $emessage = "Here is the latest $list_name.";
my $count = 0;
foreach my $line(@subscriber) {
next if $line =~ m/^\s*$/;
(my $date,$name,$email) = split /,/,$line;
my $unsubmessage = &UnsubLink;
if ($file =~ m/txt$/) {
&SendText($emessage, "$path_to_files/$file", $unsubmessage);
}
elsif ($file =~ m/(?:htm|html)$/) {
return "Sorry, unable to send HTML file, MIME::Entity not installed!
\n" unless $mime;
&SendHTML($emessage, "$path_to_files/$file", $unsubmessage);
}
else {
return "Sorry, unable to send binary file, MIME::Entity not installed!
\n" unless $mime;
&SendBinary($emessage, "$path_to_files/$file", $unsubmessage);
}
$count++;
}
&AddRecords('archive.txt', "$date,$recordfile,$count");
return "The file: '$file' was sent to $count subscribers.
\n";
}
sub SendBinary {
my ($emessage, $file, $unsubmessage) = @_;
my $top = build MIME::Entity
Type => 'multipart/mixed',
From => $our_email,
To => $email,
Subject => $list_name;
attach $top
Data => "Hello $name,\n\n$emessage\n\n$us\n\n\n$unsubmessage";
attach $top
Path => $file,
Type => 'application/octet-stream',
Encoding => 'base64';
open ('MAIL', "|$mail_prog -t -i $odq") or DieNice("Can't open mail program '$mail_prog': $!");
eval '$top->print(\*MAIL)' if $mime;
close MAIL;
return;
}
sub SendHTML {
my ($emessage, $file, $unsubmessage) = @_;
my $top = build MIME::Entity
Type => 'multipart/mixed',
From => $our_email,
To => $email,
Subject => $list_name;
attach $top
Data => "Hello $name,\n\n$emessage\n\n$us\n\n\n$unsubmessage";
attach $top
Path => $file,
Type => 'text/HTML',
Encoding => '7bit';
open ('MAIL', "|$mail_prog -t -i $odq") or DieNice("Can't open mail program '$mail_prog': $!");
eval '$top->print(\*MAIL)' if $mime;
close MAIL;
return;
}
# Send Text file to subscriber, we are using a closure
{
# we define some closure vars to provide a private memory
my $text; # remember contents of last file sent
my $last_file; # remember name of last file sent
sub SendText {
my ($emessage, $file, $unsubmessage) = @_;
# add path unless full path specified
$file = "$script_path/$file" unless $file =~ m/\//;
# first check if we have just opened this file,
# to avoid unecessary disk access on bulk mailout
if ($last_file ne $file) {
$text = join "\n", &GetRecords($file);
$last_file = $file; # remember the last file sent
}
my $message = Unindent <<" MESSAGE";
To: $email
Reply-to: <$our_email>
From: $us <$our_email>
Subject: $list_name
Hello $name,
$emessage
$text
$us
$unsubmessage
MESSAGE
open ('MAIL', "|$mail_prog -t -i $odq") or DieNice("Can't open mail program '$mail_prog': $!\n");
print MAIL $message;
close MAIL;
return;
}
}
sub Unindent {
my $unindent = shift;
$unindent =~ s/^[ \t]+//gm;
return $unindent;
}