[Mailmunge] Email parts in filter_message

list at ptld.com list at ptld.com
Wed Oct 26 11:12:19 EDT 2022


> how do I execute actions for each part?
Each part of what? The milter stages such as HELO, RCPT, SENDER, DATA? Parts of the email like headers & body?

> similar to MimeDefang
I have not used MimeDefang which might be why i don't fully understand the questions.


If you talking about being able to process the text of the email body, this is how i did it.


sub filter_message {
   my ($self, $ctx) = @_;
   ...
   my $entity = $ctx->mime_entity;
   my $mainBody = $entity->bodyhandle;
   if (defined $mainBody && scalar $entity->parts == 0) {loop_over_body($ctx, $mainBody);}
   foreach my $loop ($entity->parts) {
     return if $ctx->message_rejected();
     my $type = $loop->effective_type;
     my $body = $loop->bodyhandle;
     # Reject MIME message/partial
     if ($type =~ m"message/partial"i) {$ctx->action_bounce("MIME message/partial not allowed", 550, "5.7.1"); return;}
     # Send email body text to scanner
     if ($type =~ m"text/"i) {loop_over_body($ctx, $body);}
   }
   ...
}


sub loop_over_body {
   my ($ctx, $body) = @_;
   if (my $io = $body->open('r')) {
     while (defined(my $line = $io->getline)) {
       last if $ctx->message_rejected();
       #
       # Do what you want with $line which is one "line\n" of raw email body
       #
     }
     $io->close();
   }
}



More information about the Mailmunge mailing list