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