4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 # find Koha's Perl modules
23 # test carefully before changing this
25 eval { require "$FindBin::Bin/../kohalib.pl" };
32 use C4::Reports::Guided;
43 [--report ][--notice][--module] --library --from
47 --report report ID to use as data for email template
48 --notice specific notice code to use
49 --module which module to find the above notice in
50 --library specified branch for selecting notice, will use all libraries by default
51 --from specified email for 'from' address, report column 'from' used if not specified
52 --email specified column to use as 'to' email address, report column 'email' used if not specified
53 --verbose increased verbosity, will print notices and errors
54 --commit send emails, without this script will only report
62 Print brief help and exit.
66 Print full documentation and exit.
70 Specify a saved SQL report id in the Koha system to user for the emails. All, and only,
71 columns in the report will be available for notice template variables
75 Specific notice (CODE) to select
79 Which module to find the specified notice in
83 Option to specify which branches notice should be used, 'All libraries' is used if not specified
87 Specify the sender address of the email, if not specified a 'from' column in the report will be used.
91 Specify the column to find recipient address of the email, if not specified an 'email' column in the report will be used.
95 Increased verbosity, reports successes and errors.
99 Send emails, if omitted script will report as verbose.
108 my $module; #this is only for selecting correct notice - report itself defines available columns, not module
109 my $library; #as above, determines which notice to use, will use 'all libraries' if not specified
110 my $email; #to specify which column should be used as email in report will use 'email' from borrwers table
111 my $from; #to specify from address, will expect 'from' column in report if not specified
116 MISSING_PARAMS => "You must supply a report ID, letter module and code at minimum\n",
117 NO_LETTER => "The specified letter was not found, please check your input\n",
118 NO_REPORT => "The specified report was not found, please check your input\n",
119 REPORT_FAIL => "There was an error running the report, please check your SQL\n",
120 NO_BOR_COL => "There was no borrowernumber found for row ",
121 NO_EMAIL_COL => "There was no email found for row ",
122 NO_FROM_COL => "No from email was specified for row ",
123 NO_BOR => "There is no borrower with borrowernumber "
128 'report=i' => \$report_id,
129 'notice=s' => \$notice,
130 'module=s' => \$module,
131 'library=s' => \$library,
132 'email=s' => \$email,
134 'verbose' => \$verbose,
137 pod2usage(1) if $help;
138 pod2usage(1) unless $report_id && $notice && $module;
140 my ( $emails, $errors ) = C4::Reports::Guided::EmailReport({
143 report_id => $report_id,
151 foreach my $email (@$emails){
152 print "No emails will be sent!\n" unless $commit;
153 if( $verbose || !$commit ){
154 print "Email generated to $email->{to_address} from $email->{from_address}\n";
156 print $email->{letter}->{content} ."\n";
158 C4::Letters::EnqueueLetter({
159 letter => $email->{letter},
160 borrowernumber => $email->{borrowernumber},
161 message_transport_type => 'email',
162 from_address => $email->{from_address},
163 to_address => $email->{to_address},
167 if( $verbose || !$commit ){
168 foreach my $error ( @$errors ){
169 foreach ( keys %{$error} ){
171 if ( $_ eq 'FATAL' ) { print $error_msgs->{ ${$error}{$_} } }
172 else { print $error_msgs->{$_} . ${$error}{$_} . "\n" }