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