New to custom hooks

Mark Martinec Mark.Martinec+amavis at ijs.si
Wed Jan 27 17:56:33 CET 2016


Scott Fertig wrote:

> I've gotten past my original issues, now I am much further along, I
> also forgot to mention that I am working with amavis 2.9. The
> remaining issue I have is that I am trying to get the body of the
> message if a subject matches a variable I have set in my custom hook.
> It appears that amavis tries to get the body based on this subject
> value but the output of this ends up being IO::File=GLOB(0x6a0ef70)
> 
> sub new {
>   my($class,$conn,$msginfo) = @_;
>   my($self) = bless {}, $class;
> my($valuefrombody) = $msginfo->mail_text;
> my($line); my($line_cnt) = 0;
> my($ll) = 0;
> do_log($ll,"CUSTOM: $valuefrombody");
> 
> The log reflects this as well and the value is being stored as
> IO::File=GLOB(0x6a0f528), I'm not quite sure what I'm overlooking to
> get this everything else appears to work as intended though.
> 
> Jan 23 09:55:06 mail amavis[2730]: (02730-01) CUSTOM: 
> IO::File=GLOB(0x6a0f528)

The $msginfo->mail_text is a perl file handle, open to a temporary
file holding the entire original/pristine mail message. You may
read it and reposition it, just do not attempt to write to it
or close it.

In addition to mail_text method, there are also mail_text_str
(a ref to a string holding the entire message, when a message is
short enough to fit in memory), and there is also a mail_text_fn():

sub mail_text       # RFC 5322 msg: open file handle, or MIME::Entity 
object
   { @_<2 ? shift->{mail_text}  : ($_[0]->{mail_text} = $_[1]) }

sub mail_text_str   # RFC 5322 msg: small messages as a stringref, else 
undef
   { @_<2 ? shift->{mailtextstr}: ($_[0]->{mailtextstr} = $_[1]) }

sub mail_text_fn    # orig. mail filename or undef, e.g. 
mail_tempdir/email.txt
   { @_<2 ? shift->{mailtextfn} : ($_[0]->{mailtextfn} = $_[1]) }


> Alright, I'm not sure but as I am learning/reading I see the mail_text
> is relational to the filename of the email (please correct me if I am
> wrong). With this in mind, I see that the file is the whole email
> including the mail headers. Is there anyway that I can get amavis to
> strictly get the contents of the email without the headers? I just
> want to use the contents of the email within a .forward file without
> the headers if at all possible.

You will need to rewind the file, read (and discard) lines up to an
empty line (header/body separator), then read the rest, which is
the original mail body, doing with it whatever is needed.

Something like (untested):

   $fh->seek(0,0) or die "Can't rewind mail file: $!";
   my $in_body; my $linel;
   while ( defined($line = $fh->getline) ) {
     if ($in_body) {
       ...
     } elsif ($line eq "\n") {
       $in_body = 1;
     }
   }

This may be optimized for short messages where mail_text_str
provides a ref to a string (i.e. is not undefined).
Another possible optimization is to read the rest of the
mail message in bulk ($fh->read(...) instead of line-by-line.

   Mark


More information about the amavis-users mailing list