-  if (!$self->{"do_not_attach"}) {
-    for my $i (1 .. $self->{attfile_count}) {
-      if (  $self->{"attsel_$i"} ) {
-        my $attfile = SL::File->get(id => $self->{"attfile_$i"});
-        $main::lxdebug->message(LXDebug->DEBUG2(), "att file=".$self->{"attfile_$i"}." obj=".$attfile);
-        push @attfiles, $attfile if $attfile;
-      }
-    }
-    for my $i (1 .. $self->{attfile_cv_count}) {
-      if (  $self->{"attsel_cv_$i"} ) {
-        my $attfile = SL::File->get(id => $self->{"attfile_cv_$i"});
-        $main::lxdebug->message(LXDebug->DEBUG2(), "att file=".$self->{"attfile_$i"}." obj=".$attfile);
-        push @attfiles, $attfile if $attfile;
-      }
-    }
-    for my $i (1 .. $self->{attfile_part_count}) {
-      if (  $self->{"attsel_part_$i"} ) {
-        my $attfile = SL::File->get(id => $self->{"attfile_part_$i"});
-        $main::lxdebug->message(LXDebug->DEBUG2(), "att file=".$self->{"attfile_$i"}." obj=".$attfile);
-        push @attfiles, $attfile if $attfile;
-      }
-    }
-    foreach my $attfile ( @attfiles ) {
-      push @{ $mail->{attachments} }, { path    => $attfile->get_file,
-                                        id      => $attfile->id,
-                                        type    => $attfile->mime_type,
-                                        name    => $attfile->file_name,
-                                        content => $attfile->get_content };
-    }
+
+  push @attfiles,
+    grep { $_ }
+    map  { SL::File->get(id => $_) }
+    @{ $self->{attach_file_ids} // [] };
+
+  foreach my $attfile ( @attfiles ) {
+    push @{ $mail->{attachments} }, {
+      path    => $attfile->get_file,
+      id      => $attfile->id,
+      type    => $attfile->mime_type,
+      name    => $attfile->{override_file_name} // $attfile->file_name,
+      content => $attfile->get_content ? ${ $attfile->get_content } : undef,
+    };