Chris Nighswonger wrote:

I believe that there has been an effort over the development of Koha
to separate the Koha administrative functions from the system
administrative functions. Since mail setup falls squarely in the
system administrative side of things, it might be better to write a
test script which could be run from the cli.

I agree with you Chris.

Attached is such a script, to run from the command line.

Anyone care to test it out? Works fine on my Koha systems.


cheers
rick



--
_________________________________
Rick Welykochy || Praxis Services

Politics is the business of getting power and privilege without
possessing merit.
     -- PJ O'Rourke
#!/usr/bin/env perl

# This script tests the Mail::Sendmail mailing service which is used internally
# by Koha.  The script sends a test email to one or more recipients and displays
# the result of the test.

# Copyright 2010 Rick Welykochy, NSW, Australia
#
# This file is part of Koha.
#
# Koha 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.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA  02111-1307 USA

use strict;
use warnings;
use Mail::Sendmail qw(sendmail %mailcfg);
use Getopt::Long;
use Sys::Hostname;

sub help
{
        print <<ENDHERE;
$0: test sending email using the Koha method, which uses Mail::Sendmail.

use: $0 [ option ]... recipient...

         recipient ...    one or more email recipients

options:
   -?  or --help          print this help message and exit
   -v  or --verbose       be verbose and also turn debugging on in 
Mail::Sendmail
   -n  or --nomail        do not send email, just show what would be done


Contents of Mail::Sendmail::mailcfg:

ENDHERE

        for my $key (sort keys %mailcfg) {
                my $value = $mailcfg{$key};
                $value = "@$value" if ref($value) && ref($value) eq "ARRAY";
                printf "%-25s %s\n", "$key =", $value;
        }
        return 1;
}

sub dump_mail
{
        my %params = @_;
        my $message = '';
        print "Email dump:\n";
        for my $key (keys %params) {
                if ($key =~ /^message$|^body$|^text$/i) {
                        $message = $params{$key};
                        next;
                }
                printf "%s: %s\n", $key, $params{$key};
        }
        print "\n";
        print "$message\n" if $message;
}

sub main
{
        my $return  = 0;
        my $help    = 0;
        my $verbose = 0;
        my $nomail  = 0;

        my %options = (
                'help|?'        => \$help,
                'verbose|v'     => sub { $mailcfg{debug} = 6; $verbose = 1; },
                'nomail|n'      => \$nomail
        );

        if (!GetOptions(%options)) {
                print STDERR "$0: unrecognised command line argument. Try 
--help for help\n";
                exit 1;
        }

        return help if $help;

        my @recipients = @ARGV;
        if (!...@recipients) {
                print STDERR "$0: missing email recipient. Try --help for 
help\n";
                exit 1;
        }

        my $from        = "postmaster\@".hostname
        my $to          = $recipients[0];
        my $cc          = @recipients > 1 ? join(", 
",@recipients[...@recipients-1]) : undef;
        my $subject     = "Test email";
        my $message     = "Hello. This is a test email, sent by the Koha script 
$0.\n";

        my %mail = (
                To              => $to,
                From            => $from,
                Subject         => $subject,
                Message         => $message,
                'X-Mailer'      => "Mail::Sendmail version 
$Mail::Sendmail::VERSION",
        );
        $mail{Cc} = $cc if $cc;

        dump_mail(%mail) if $nomail || $verbose;

        if ($nomail) {
                print "\nEmail not sent (--nomail)\n";
        } else {
                $return = sendmail(%mail);
                print "Email log: $Mail::Sendmail::log\n" if 
$Mail::Sendmail::log && $verbose;
                print "Email error: $Mail::Sendmail::error\n" if 
$Mail::Sendmail::error;
                print "Email sent to @recipients.\n" if $return == 1;
        }       
        
        return $return;
}

exit main;


_______________________________________________
Koha-devel mailing list
Koha-devel@lists.koha.org
http://lists.koha.org/mailman/listinfo/koha-devel

Reply via email to