View | Details | Raw Unified | Return to bug 36766
Collapse All | Expand All

(-)a/installer/data/mysql/atomicupdate/bug_36766-add_SFTP_notices.pl (+20 lines)
Line 0 Link Here
1
use Modern::Perl;
2
3
return {
4
    bug_number  => "36766",
5
    description => "Add command-line utility to SFTP a file to a remote server",
6
    up          => sub {
7
        my ($args) = @_;
8
        my ( $dbh, $out ) = @$args{qw(dbh out)};
9
10
        $dbh->do(q{
11
            INSERT IGNORE INTO letter
12
            (module,code,branchcode,name,is_html,title,content,message_transport_type,lang)
13
            VALUES
14
            ('commandline', 'SFTP_FAILURE', '', 'File SFTP failed', 0, 'The SFTP by sftp_file.pl failed', 'SFTP upload failed:\n\n<<sftp_status>>', 'email', 'default'),
15
            ('commandline', 'SFTP_SUCCESS', '', 'File SFTP success', 0, 'The SFTP by sftp_file.pl was successful', 'SFTP upload succeeded', 'email', 'default')
16
        });
17
18
        say $out "Added new sample notices 'SFTP_FAILURE' and 'SFTP_SUCCESS'";
19
    },
20
};
(-)a/installer/data/mysql/en/mandatory/sample_notices.yml (+24 lines)
Lines 964-969 tables: Link Here
964
            - ""
964
            - ""
965
            - "<order>Ordernumber <<aqorders.ordernumber>> (<<biblio.title>>) (<<aqorders.quantity>> ordered) ($<<aqorders.listprice>> each) has not been received.</order>"
965
            - "<order>Ordernumber <<aqorders.ordernumber>> (<<biblio.title>>) (<<aqorders.quantity>> ordered) ($<<aqorders.listprice>> each) has not been received.</order>"
966
966
967
        - module: commandline
968
          code: SFTP_FAILURE
969
          branchcode: ""
970
          name: "File SFTP failed"
971
          is_html: 0
972
          title: "The SFTP by sftp_file.pl failed"
973
          message_transport_type: email
974
          lang: default
975
          content:
976
            - "SFTP upload failed:"
977
            - ""
978
            - "<<sftp_status>>"
979
980
        - module: commandline
981
          code: SFTP_SUCCESS
982
          branchcode: ""
983
          name: "File SFTP success"
984
          is_html: 0
985
          title: "The SFTP by sftp_file.pl was successful"
986
          message_transport_type: email
987
          lang: default
988
          content:
989
            - "SFTP upload succeeded"
990
967
        - module: ill
991
        - module: ill
968
          code: ILL_PICKUP_READY
992
          code: ILL_PICKUP_READY
969
          branchcode: ""
993
          branchcode: ""
(-)a/misc/cronjobs/sftp_file.pl (-26 / +20 lines)
Lines 23-28 use Koha::Email; Link Here
23
use Getopt::Long qw( GetOptions );
23
use Getopt::Long qw( GetOptions );
24
use Pod::Usage qw( pod2usage );
24
use Pod::Usage qw( pod2usage );
25
use Carp qw( carp );
25
use Carp qw( carp );
26
use C4::Letters qw( GetPreparedLetter EnqueueLetter );
26
27
27
=head1 NAME
28
=head1 NAME
28
29
Lines 178-191 if ( $email ) { Link Here
178
    if ( !Koha::Email->is_valid($email) ) {
179
    if ( !Koha::Email->is_valid($email) ) {
179
        return warn "The email address you defined in the --email parameter is invalid\n";
180
        return warn "The email address you defined in the --email parameter is invalid\n";
180
    }
181
    }
181
182
    $status_email = Koha::Email->new(
183
        {
184
            to        => $email,
185
            from      => $admin_address,
186
            subject   => '[OCLC] ' . C4::Context->config('database') . ' ' . $sftp_status,
187
        }
188
    );
189
}
182
}
190
183
191
# Do the SFTP upload
184
# Do the SFTP upload
Lines 196-226 if ( Link Here
196
            callback => sub { my ( $sftp, $data, $offset, $size ) = @_; warn "$offset of $size bytes transferred\n"; }
189
            callback => sub { my ( $sftp, $data, $offset, $size ) = @_; warn "$offset of $size bytes transferred\n"; }
197
        )
190
        )
198
) {
191
) {
199
    $sftp_status = 'successful';
200
    # Send success email
192
    # Send success email
201
    if ( $email ) {
193
    $sftp_status = 'SUCCESS';
202
        $status_email_message = "OCLC upload was successful";
203
    }
204
    close $fh;
194
    close $fh;
205
} else {
195
} else {
206
    $sftp_status = 'failed';
207
    # Send failure email
196
    # Send failure email
208
    if ( $email ) {
197
    $sftp_status = 'FAILURE';
209
        $status_email_message = "OCLC upload failed\nOCLC error: " . $sftp->error;
210
    }
211
}
198
}
212
199
213
# Send email confirming the success or failure of the SFTP
200
# Send email confirming the success or failure of the SFTP
214
if ( $email ) {
201
if ( $email ) {
215
    $status_email->text_body($status_email_message);
202
    $status_email =  C4::Letters::GetPreparedLetter (
216
    my $smtp_server = Koha::SMTP::Servers->get_default;
203
        module => 'commandline',
217
    $email->transport( $smtp_server->transport );
204
        letter_code => "SFTP_$sftp_status", #SFTP_SUCCESS, SFTP_FAILURE
218
    try {
205
        message_transport_type => 'email',
219
        $email->send_or_die;
206
        substitute => {
220
    }
207
            sftp_error => $sftp->error
221
    catch {
208
        }
222
        carp "Mail not sent: $_";
209
    );
223
    };
210
211
    C4::Letters::EnqueueLetter(
212
        {
213
            letter                  => $status_email,
214
            to_address              => $email,
215
            from_address            => $admin_address,
216
            message_transport_type  => 'email'
217
        }
218
    ) or warn "can't enqueue letter " . $status_email->{code};
224
}
219
}
225
220
226
cronlogaction({ action => 'End', info => "COMPLETED" });
221
cronlogaction({ action => 'End', info => "COMPLETED" });
227
- 

Return to bug 36766