Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Filehandle with DKIM::Verifier

by nifu (Initiate)
on Oct 14, 2017 at 23:32 UTC ( #1201385=perlquestion: print w/replies, xml ) Need Help??
nifu has asked for the wisdom of the Perl Monks concerning the following question:

Hello, thanks for reading my question and sorry for my bad english. I use a perl script to analyze differed headers from emails (saved as txt file). The script search every $x hours via cronjob in the directory for new emails. Like perl $email.filename.txt The perl script must now check for a valid dkim signature. I did some tests with Mail:: DKIM:: Verifier. But it only works when i read the file from stdin like perl < email.txt. I donīt know how to use the perl module with $dkim->load(*STDIN);. I have tried to load the file with a filehandle like $dkim->load(<fh>); or $dkim->load($filename);. But it doesn't work. Has someone an idea how i can solve this?

Replies are listed 'Best First'.
Re: Filehandle with DKIM::Verifier
by kcott (Chancellor) on Oct 15, 2017 at 01:57 UTC

    G'day nifu,

    Welcome to the Monastery.

    Your problem description is unclear. Simply showing individual lines of code, in isolation, with no reference to their context, does not help us to help you. Please read "How do I post a question effectively?" and "SSCCE".

    "I donīt know how to use the perl module with $dkim->load(*STDIN);. ..."

    The documentation for Mail::DKIM::Verifier starts with an example of that (in the Synopsis); the load() section further down has details. Is some part of that unclear to you?

    Reading between the lines, I suspect you may be confusing "<FILEHANDLE>" and "<>" (the special null filehandle).

    I created a test file:

    $ cat > XXX qwe rty

    With "<>", you get the behaviour I think you're looking for:

    $ perl -e 'while (<>) { print }' XXX qwe rty

    With "<STDIN>", instead of "<>", the above code is waiting for input from stdin. In this example, I issued an interrupt (after waiting a few seconds for something to happen):

    $ perl -e 'while (<STDIN>) { print }' XXX ^C

    In the next example, I used the keyboard for stdin. Note the absence of any content from "XXX".

    $ perl -e 'while (<STDIN>) { print }' XXX typed typed from from keyboard keyboard

    You can get the contents of "XXX" via redirection or piping:

    $ perl -e 'while (<STDIN>) { print }' < XXX qwe rty
    $ cat XXX | perl -e 'while (<STDIN>) { print }' qwe rty

    Take a look at "perlop: I/O Operators" and "perlvar: Variables related to filehandles" for more complete details on this subject.

    — Ken

Re: Filehandle with DKIM::Verifier
by LanX (Bishop) on Oct 15, 2017 at 02:30 UTC
    Hello nifu and welcome to the monastery

    > $dkim->load(*STDIN); I have tried to load the file with a filehandle like $dkim->load(<fh>); or $dkim->load($filename); .

    I think you are confusing filehandle with filename.

    You need to open a filehandle to a file (and close it afterwards)

    Something like

    open(my $fh, "<", "$filename") or die "Can't open < $filename: $!" $dkim->load($fh); ...

    should do. (Untested)

    HTH :)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Hello everyone and thanks for the answers.

      This is the code form my small test script:

      #! /usr/bin/perl use strict; use warnings; use Mail::DKIM::Verifier; my $raw_email; my $dkim = Mail::DKIM::Verifier->new(); open (MESSAGE, "< $ARGV[0]") || die "Couldn't open email $ARGV[0]\n"; undef $/; $raw_email = <>; close MESSAGE; print "$ARGV"; open(my $fh, "<", "$ARGV") or die "Can't open < $ARGV: $!"; $dkim->load($fh); my $result = $dkim->result; print "\n$result"; foreach my $signature ($dkim->signatures) { print "signature identity: " . $signature->identity . "\n"; print "verify result: " . $signature->result_detail . "\n"; }

      I have tested this script with differed emails. Test files with correct, wrong and missing DKIM signature. The script returns always "none" (my $result = $dkim->result;) and $signature is an empty variable.

      It seams that this script canīt find a DKIM signature in the test files.

        open (MESSAGE, "< $ARGV[0]") || die "Couldn't open email $ARGV[0]\n"; undef $/; $raw_email = <>; close MESSAGE;

        Hello, nifu. Could you please explain what this section of code is doing and why?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1201385]
Approved by holli
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2018-03-20 00:05 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (246 votes). Check out past polls.