This simply wasnt working before.
[koha.git] / opac / opac-sendbasket.pl
1 #!/usr/bin/perl
2 use strict;
3 require Exporter;
4 use CGI;
5 use Mail::Sendmail;
6 use MIME::QuotedPrint;
7 use MIME::Base64;
8 use C4::Context;
9 use C4::Search;
10 use C4::Auth;
11 use C4::Interface::CGI::Output;
12 use C4::Biblio;
13 use HTML::Template;
14
15 my $query = new CGI;
16
17 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
18     {
19         template_name   => "opac-sendbasketform.tmpl",
20         query           => $query,
21         type            => "opac",
22         authnotrequired => 1,
23         flagsrequired   => { borrow => 1 },
24     }
25 );
26
27 my $bib_list     = $query->param('bib_list');
28 my $email_add    = $query->param('email_add');
29 my $email_sender = $query->param('email_sender');
30
31 my $dbh = C4::Context->dbh();
32
33 if ($email_add) {
34     my $email_from = C4::Context->preference('KohaAdminEmailAddress');
35
36     my %mail = (
37         To   => $email_add,
38         From => $email_from
39     );
40
41     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
42         {
43             template_name   => "opac-sendbasket.tmpl",
44             query           => $query,
45             type            => "opac",
46             authnotrequired => 1,
47             flagsrequired   => { borrow => 1 },
48         }
49     );
50
51     my @bibs = split ( /\//, $bib_list );
52     my @results;
53     my $iso2709;
54     foreach my $biblionumber (@bibs) {
55         $template2->param( biblionumber => $biblionumber );
56
57         my $dat = &bibdata($biblionumber);
58         my ( $authorcount, $addauthor ) = &addauthor($biblionumber);
59         my @items = &ItemInfo( undef, $biblionumber, 'opac' );
60
61         $dat->{'additional'} = $addauthor->[0]->{'author'};
62         for ( my $i = 1 ; $i < $authorcount ; $i++ ) {
63             $dat->{'additional'} .= "|" . $addauthor->[$i]->{'author'};
64         }
65
66         $dat->{'biblionumber'} = $biblionumber;
67         $dat->{ITEM_RESULTS} = \@items;
68         my $record = MARCgetbiblio( $dbh, $biblionumber );
69         $iso2709 .= $record->as_usmarc();
70
71         push ( @results, $dat );
72     }
73
74     my $resultsarray = \@results;
75     $template2->param(
76         BIBLIO_RESULTS => $resultsarray,
77         email_sender   => $email_sender
78     );
79
80     # Getting template result
81     my $template_res = $template2->output();
82
83     # Analysing information and getting mail properties
84     if ( $template_res =~ /<SUBJECT>\n(.*)\n<END_SUBJECT>/s ) {
85         $mail{'subject'} = $1;
86     }
87     else { $mail{'subject'} = "no subject"; }
88
89     my $email_header = "";
90     if ( $template_res =~ /<HEADER>\n(.*)\n<END_HEADER>/s ) {
91         $email_header = $1;
92     }
93
94     my $email_file = "basket.txt";
95     if ( $template_res =~ /<FILENAME>\n(.*)\n<END_FILENAME>/s ) {
96         $email_file = $1;
97     }
98
99     if ( $template_res =~ /<MESSAGE>\n(.*)\n<END_MESSAGE>/s ) {
100         $mail{'body'} = $1;
101     }
102
103     my $boundary = "====" . time() . "====";
104     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
105
106     $email_header = encode_qp($email_header);
107
108     $boundary = "--" . $boundary;
109
110     # Writing mail
111     $mail{body} = <<END_OF_BODY;
112 $boundary
113 Content-Type: text/plain; charset="iso-8859-1"
114 Content-Transfer-Encoding: quoted-printable
115
116 $email_header
117
118 $mail{'body'}
119
120 $boundary--
121 END_OF_BODY
122
123     # Sending mail
124     if ( sendmail %mail ) {
125
126         # do something if it works....
127         warn "Mail sent ok\n";
128         $template->param( SENT      => "1" );
129         $template->param( email_add => $email_add );
130     }
131     else {
132
133         # do something if it doesnt work....
134         warn "Error sending mail: $Mail::Sendmail::error \n";
135         warn "$mail{'body'}";
136     }
137
138     output_html_with_http_headers $query, $cookie, $template->output;
139 }
140 else {
141     $template->param( bib_list => $bib_list );
142     $template->param(
143         url            => "/cgi-bin/koha/opac-sendbasket.pl",
144         suggestion     => C4::Context->preference("suggestion"),
145         virtualshelves => C4::Context->preference("virtualshelves"),
146     );
147     output_html_with_http_headers $query, $cookie, $template->output;
148 }