bug 3141: use quoted printable encoding in cart/list emails
[koha.git] / opac / opac-sendbasket.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 use strict;
19
20 use CGI;
21 use Encode qw(encode);
22
23 use Mail::Sendmail;
24 use MIME::QuotedPrint;
25 use MIME::Base64;
26 use C4::Biblio;
27 use C4::Items;
28 use C4::Auth;
29 use C4::Output;
30 use C4::Biblio;
31
32 my $query = new CGI;
33
34 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
35     {
36         template_name   => "opac-sendbasketform.tmpl",
37         query           => $query,
38         type            => "opac",
39         authnotrequired => 1,
40         flagsrequired   => { borrow => 1 },
41     }
42 );
43
44 my $bib_list     = $query->param('bib_list');
45 my $email_add    = $query->param('email_add');
46 my $email_sender = $query->param('email_sender');
47
48 my $dbh          = C4::Context->dbh;
49
50 if ( $email_add ) {
51     my $email_from = C4::Context->preference('KohaAdminEmailAddress');
52     my $comment    = $query->param('comment');
53     my %mail = (
54         To   => $email_add,
55         From => $email_from
56     );
57
58     my ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
59         {
60             template_name   => "opac-sendbasket.tmpl",
61             query           => $query,
62             type            => "opac",
63             authnotrequired => 1,
64             flagsrequired   => { borrow => 1 },
65         }
66     );
67
68     my @bibs = split( /\//, $bib_list );
69     my @results;
70     my $iso2709;
71     my $marcflavour = C4::Context->preference('marcflavour');
72     foreach my $biblionumber (@bibs) {
73         $template2->param( biblionumber => $biblionumber );
74
75         my $dat              = GetBiblioData($biblionumber);
76         my $record           = GetMarcBiblio($biblionumber);
77         my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
78         my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
79         my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
80
81         my @items = &GetItemsInfo( $biblionumber, 'opac' );
82
83         $dat->{MARCNOTES}      = $marcnotesarray;
84         $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
85         $dat->{MARCAUTHORS}    = $marcauthorsarray;
86         $dat->{'biblionumber'} = $biblionumber;
87         $dat->{ITEM_RESULTS}   = \@items;
88
89         $iso2709 .= $record->as_usmarc();
90
91         push( @results, $dat );
92     }
93
94     my $resultsarray = \@results;
95     $template2->param(
96         BIBLIO_RESULTS => $resultsarray,
97         email_sender   => $email_sender,
98         comment        => $comment
99     );
100
101     # Getting template result
102     my $template_res = $template2->output();
103     my $body;
104
105     # Analysing information and getting mail properties
106     if ( $template_res =~ /<SUBJECT>\n(.*)\n<END_SUBJECT>/s ) {
107         $mail{'subject'} = $1;
108     }
109     else { $mail{'subject'} = "no subject"; }
110
111     my $email_header = "";
112     if ( $template_res =~ /<HEADER>\n(.*)\n<END_HEADER>/s ) {
113         $email_header = $1;
114     }
115
116     my $email_file = "basket.txt";
117     if ( $template_res =~ /<FILENAME>\n(.*)\n<END_FILENAME>/s ) {
118         $email_file = $1;
119     }
120
121     if ( $template_res =~ /<MESSAGE>\n(.*)\n<END_MESSAGE>/s ) { $body = encode_qp($1); }
122
123     my $boundary = "====" . time() . "====";
124
125     #     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
126     #
127     #     $email_header = encode_qp($email_header);
128     #
129     #     $boundary = "--".$boundary;
130     #
131     #     # Writing mail
132     #     $mail{body} =
133     $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
134     my $isofile = encode_base64(encode("UTF-8", $iso2709));
135     $boundary = '--' . $boundary;
136     $mail{body} = <<END_OF_BODY;
137 $boundary
138 Content-Type: text/plain; charset="utf-8"
139 Content-Transfer-Encoding: quoted-printable
140
141 $email_header
142 $body
143 $boundary
144 Content-Type: application/octet-stream; name="basket.iso2709"
145 Content-Transfer-Encoding: base64
146 Content-Disposition: attachment; filename="basket.iso2709"
147
148 $isofile
149 $boundary--
150 END_OF_BODY
151
152     # Sending mail
153     if ( sendmail %mail ) {
154         # do something if it works....
155         $template->param( SENT      => "1" );
156     }
157     else {
158         # do something if it doesnt work....
159         warn "Error sending mail: $Mail::Sendmail::error \n";
160         $template->param( error => 1 );
161     }
162     $template->param( email_add => $email_add );
163     output_html_with_http_headers $query, $cookie, $template->output;
164 }
165 else {
166     $template->param( bib_list => $bib_list );
167     $template->param(
168         url            => "/cgi-bin/koha/opac-sendbasket.pl",
169         suggestion     => C4::Context->preference("suggestion"),
170         virtualshelves => C4::Context->preference("virtualshelves"),
171     );
172     output_html_with_http_headers $query, $cookie, $template->output;
173 }