#!/usr/bin/perl # Copyright 2001-2009 Leslie Richardson # This file is part of Open Admin for Schools. # Open Admin for Schools is free software; you can redistribute it # and/or modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. my %lex = ('Group Email' => 'Group Email', 'Grade' => 'Grade', 'Homeroom' => 'Homeroom', 'Main' => 'Main', 'Blank=All' => 'Blank=All', 'Select' => 'Select', 'Values' => 'Values', 'Group' => 'Group', 'Parent 1' => 'Parent 1', 'Parent 2' => 'Parent 2', 'Separate with Spaces' => 'Separate with Spaces', 'Subject' => 'Subject', 'Message' => 'Message', 'Continue' => 'Continue', 'Student' => 'Student', 'Checked' => 'Checked', 'Email Sent' => 'Email Sent', 'Error' => 'Error', 'Email' => 'Email', 'Attach File' => 'Attach File', 'Cannot open file' => 'Cannot open file', 'Maximum File Upload size exceeded!' => 'Maximum File Upload size exceeded!', ); my $self = 'emailgroup.pl'; # Configured Values my $maxbufcount = 1000; # 1000K limit my $mailserver = 'richtech.ca'; # localhost my $noreplyName = 'OpenAdmin Messaging System'; my $noreplyEmail = 'no-reply@yoursite.net'; use CGI; use DBI; use Mail::Sender; # calc current date my @tim = localtime(time); my $year = @tim[5] + 1900; my $month = @tim[4] + 1; my $day = @tim[3]; my $currdate = "$year-$month-$day"; eval require "../etc/admin.conf"; if ( $@ ) { print $lex{Error}. " :$@
\n"; die $lex{Error}. " :$@
\n"; } my $q = new CGI; print $q->header( -charset, $charset ); my %arr = $q->Vars; my $checked = $arr{checked}; delete $arr{checked}; my $dsn = "DBI:$dbtype:dbname=$dbase"; my $dbh = DBI->connect($dsn,$user,$password); $dbh->{mysql_enable_utf8} = 1; # Show page Header print "$doctype\n". $lex{'Group Email'}. "\n"; print "\n"; print "$chartype\n\n"; print "[ ". $lex{Main}. " ]\n"; print "

". $lex{'Group Email'}. "

\n"; if ( not $arr{page} ) { # Select Groups and which emails to do (Student, Parents) showStartPage(); } elsif ( $arr{page} == 1 ) { # Select Students, Parents / Set Message, Attachment delete $arr{page}; selectRecipients(); } elsif ( $arr{page} == 2 ) { # post the Mail delete $arr{page}; postEmail(); } #---------------- sub showStartPage { #---------------- # Setup the form and start of table. print "
\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "
". $lex{Group}. "\n"; print "\n"; print "
". $lex{Values}. "\n"; print "
\n"; print $lex{'Separate with Spaces'}. q{, }. $lex{'Blank=All'}. "
". $lex{Select}. "\n"; print " "; print $lex{Student}. q{ }. $lex{Email}. "
\n"; print " "; print $lex{'Parent 1'}. q{ }. $lex{Email}. "
\n"; print " "; print $lex{'Parent 2'}. q{ }. $lex{Email}. "
\n"; print "
". $lex{Checked}. "?\n"; print "
"; print ""; print "
\n"; print "\n"; exit; } #------------------- sub selectRecipients { # Choose students/parents to email to and put in message #------------------- # foreach my $key (keys %arr) { print "K:$key V:$arr{$key}
\n"; } # Get passed group values my @group = split /\s/, $arr{groupValue}; # split into array based on spaces delete $arr{groupValue}; #print "Group", @group, "
\n"; my $groupType; if ( $arr{groupType} eq $lex{Grade} ) { $groupType = 'grade'; } else { $groupType = 'homeroom'; } delete $arr{groupType}; # Select students, sorted by lastname, firstname, my $select; if ( @group ) { $select = 'where '; my $first = 1; foreach my $value ( @group ) { $value = $dbh->quote( $value ); if ( not $first ) { $select .= ' or '; } $select .= "$groupType = $value "; $first = 0; } } if ($select) { print "Select: $select
\n"; } my $sth = $dbh->prepare("select lastname, firstname, studnum, email, par1_email, par2_email from student $select order by lastname, firstname"); $sth->execute; if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; } # Setup the form and start of table. print "
\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; # Loop through the students while ( my ($lastname, $firstname, $studnum, $email, $par1_email, $par2_email ) = $sth->fetchrow ) { print "\n"; } # Print Submit Row print "\n"; print "
". $lex{Subject}; print "
". $lex{Message}; print "
". $lex{'Attach File'}; print "
$lastname, $firstname ($studnum)\n"; if ( $email and $arr{selStudent} ) { print $lex{Student}; print " $email  | "; } if ( $par1_email and $arr{selPar1} ) { print $lex{'Parent 1'}; print " "; print "$par1_email  | "; } if ( $par2_email and $arr{selPar2} ) { print $lex{'Parent 2'}; print ""; print "$par2_email  | "; } print "
"; print "
\n
\n"; exit; } #------------ sub postEmail { #------------ my $subject = $arr{subject}; delete $arr{subject}; my $message = $arr{message}; delete $arr{message}; #$message =~ s/[\r\n|\r]/\n/g; # don't mess with CRLF. my @lines = split /\n/, $message; my $file = $q->param("filename"); my $filename = $file; # fileName is output filename, file is input. delete $arr{filename}; # value not needed. my ($name, $ext); if ( $filename ) { # it will not have leading path info... $filename =~ s!^.*(\\|\/)!!; $filename = lc($filename); @name = split /\./, $filename; # split on dots. $ext = $name[$#name]; # last element is the extension. # unless ( $ext eq 'csv' ){ # print "". $lex{'The file must be a .csv file!'}. ""; # print "\n"; # die; # } open ( OUTFILE, ">$filename") || die $lex{'Cannot open file'}. " $filename"; my $bufcount = 0; while ( my $bytesread = read( $file, my $buffer, 1024) ) { print OUTFILE $buffer; $bufcount++; if ( $bufcount > $maxbufcount ) { print "

". $lex{'Maximum File Upload size exceeded!'}; print " ($maxbufcount K)

\n"; print "\n"; die $lex{'Maximum File Upload size exceeded!'}; } } close OUTFILE; # We should now have the file in place. # my @cmd = split / /, "mv $filename $downloaddir"; # system( @cmd ); } # end of File upload. # Setup to send mail. my $fromString = "$noreplyName <$noreplyEmail>"; my $sender = new Mail::Sender { from => $fromString, smtp => $mailserver, subject => $subject, keepconnection => 1, on_errors => 'code' }; die $lex{Error}. ": $Mail::Sender::Error\n" unless ref $sender; print "
\n"; print "

". $lex{'Email Sent'}. "

\n"; print "

[ ". $lex{Main}. " ]

\n"; print "\n"; exit; } # End of postMail