+ close OUT if $self->{OUT};
+ # check only one flag (webdav_documents)
+ # therefore copy to webdav, even if we do not have the webdav feature enabled (just archive)
+ my $copy_to_webdav = $::instance_conf->get_webdav_documents && !$self->{preview} && $self->{tmpdir} && $self->{tmpfile} && $self->{type}
+ && $self->{type} ne 'statement';
+ if ( $ext_for_format eq 'pdf' && $self->doc_storage_enabled ) {
+ $self->append_general_pdf_attachments(filepath => $self->{tmpdir}."/".$self->{tmpfile},
+ type => $self->{type});
+ }
+ if ($self->{media} eq 'file') {
+ copy(join('/', $self->{cwd}, $userspath, $self->{tmpfile}), $out =~ m|^/| ? $out : join('/', $self->{cwd}, $out)) if $template->uses_temp_file;
+
+ if ($copy_to_webdav) {
+ if (my $error = Common::copy_file_to_webdav_folder($self)) {
+ chdir("$self->{cwd}");
+ $self->error($error);
+ }
+ }
+
+ if (!$self->{preview} && $self->doc_storage_enabled)
+ {
+ $self->{attachment_filename} ||= $self->generate_attachment_filename;
+ $self->store_pdf($self);
+ }
+ $self->cleanup;
+ chdir("$self->{cwd}");
+
+ $::lxdebug->leave_sub();
+
+ return;
+ }
+
+ if ($copy_to_webdav) {
+ if (my $error = Common::copy_file_to_webdav_folder($self)) {
+ chdir("$self->{cwd}");
+ $self->error($error);
+ }
+ }
+
+ if ( !$self->{preview} && $ext_for_format eq 'pdf' && $self->doc_storage_enabled) {
+ $self->{attachment_filename} ||= $self->generate_attachment_filename;
+ my $file_obj = $self->store_pdf($self);
+ $self->{print_file_id} = $file_obj->id if $file_obj;
+ }
+ if ($self->{media} eq 'email') {
+ if ( getcwd() eq $self->{"tmpdir"} ) {
+ # in the case of generating pdf we are in the tmpdir, but WHY ???
+ $self->{tmpfile} = $userspath."/".$self->{tmpfile};
+ chdir("$self->{cwd}");
+ }
+ $self->send_email(\%::myconfig,$ext_for_format);
+ }
+ else {
+ $self->{OUT} = $out;
+ $self->{OUT_MODE} = $out_mode;
+ $self->output_file($template->get_mime_type,$command_formatter);
+ }
+ delete $self->{print_file_id};
+
+ $self->cleanup;
+
+ chdir("$self->{cwd}");
+ $main::lxdebug->leave_sub();
+}
+
+sub get_bcc_defaults {
+ my ($self, $myconfig, $mybcc) = @_;
+ if (SL::DB::Default->get->bcc_to_login) {
+ $mybcc .= ", " if $mybcc;
+ $mybcc .= $myconfig->{email};
+ }
+ my $otherbcc = SL::DB::Default->get->global_bcc;
+ if ($otherbcc) {
+ $mybcc .= ", " if $mybcc;
+ $mybcc .= $otherbcc;
+ }
+ return $mybcc;
+}
+
+sub send_email {
+ $main::lxdebug->enter_sub();
+ my ($self, $myconfig, $ext_for_format) = @_;
+ my $mail = Mailer->new;
+
+ map { $mail->{$_} = $self->{$_} }
+ qw(cc subject message format);
+
+ $mail->{bcc} = $self->get_bcc_defaults($myconfig, $self->{bcc});
+ $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
+ $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
+ $mail->{fileid} = time() . '.' . $$ . '.';
+ my $full_signature = $self->create_email_signature();
+ $full_signature =~ s/\r//g;
+
+ $mail->{attachments} = [];
+ my @attfiles;
+ # if we send html or plain text inline
+ if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
+ $mail->{content_type} = "text/html";
+ $mail->{message} =~ s/\r//g;
+ $mail->{message} =~ s{\n}{<br>\n}g;
+ $full_signature =~ s{\n}{<br>\n}g;
+ $mail->{message} .= $full_signature;
+
+ open(IN, "<", $self->{tmpfile})
+ or $self->error($self->cleanup . "$self->{tmpfile} : $!");
+ $mail->{message} .= $_ while <IN>;
+ close(IN);
+
+ } elsif (($self->{attachment_policy} // '') ne 'no_file') {
+ my $attachment_name = $self->{attachment_filename} || $self->{tmpfile};
+ $attachment_name =~ s{\.(.+?)$}{.${ext_for_format}} if ($ext_for_format);
+
+ if (($self->{attachment_policy} // '') eq 'old_file') {
+ my ( $attfile ) = SL::File->get_all(object_id => $self->{id},
+ object_type => $self->{formname},
+ file_type => 'document');
+
+ if ($attfile) {
+ $attfile->{override_file_name} = $attachment_name if $attachment_name;
+ push @attfiles, $attfile;
+ }