+ my %mime = (
+ Data => $outbuf,
+ Type => $mason_request->notes('header-content-type')
+ || 'text/html',
+ Disposition => 'inline',
+ );
+ if (my $disp = $mason_request->notes('header-content-disposition') ) {
+ $disp =~ /^(attachment|inline)\s*;\s*filename=(.*)$/;
+ $mime{Disposition} = $1;
+ my $filename = $2;
+ $filename =~ s/^"(.*)"$/$1/;
+ $mime{Filename} = $filename;
+ }
+ if ($mime{Type} =~ /^text/) {
+ $mime{Encoding} = 'quoted-printable';
+ } else {
+ $mime{Encoding} = 'base64';
+ }
+ return MIME::Entity->build(%mime);
+}
+
+=item send
+
+Sends the search by email. If anything fails, logs and returns an error.
+
+=cut
+
+sub send {
+ my $self = shift;
+ my $log = FS::Log->new('FS::saved_search::send');
+ my $conf = FS::Conf->new;
+ my $user = $self->access_user;
+ my $username = $user->username;
+ my $user_email = $user->option('email_address');
+ my $error;
+ if (!$user_email) {
+ $error = "User '$username' has no email address.";
+ $log->error($error);
+ return $error;
+ }
+ $log->debug('Rendering saved search');
+ my $part = $self->render;
+
+ my %email_param = (
+ 'from' => $conf->config('invoice_from'),
+ 'to' => $user_email,
+ 'subject' => $self->searchname,
+ 'nobody' => 1,
+ 'mimeparts' => [ $part ],
+ );
+
+ $log->debug('Sending to '.$user_email);
+ $error = send_email(%email_param);
+
+ # update the timestamp
+ $self->set('last_sent', time);
+ $error ||= $self->replace;
+ if ($error) {
+ $log->error($error);
+ return $error;
+ }
+
+}
+
+sub queueable_send {
+ my $searchnum = shift;
+ my $self = FS::saved_search->by_key($searchnum)
+ or die "searchnum $searchnum not found\n";
+ $self->send;