Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Help with IO::Scalar and piping

by Corion (Pope)
on Sep 26, 2009 at 17:50 UTC ( #797713=note: print w/replies, xml ) Need Help??


in reply to Help with IO::Scalar and piping

Shouldn't that be the following?

my( $mail ) = Mail::DKIM::Verifier->new(); $mail->load( $raw_iohandle );

Because

my( $mail ) = load Mail::DKIM::Verifier->new();

is the same as

my( $mail ) = Mail::DKIM::Verifier->load->new();

which passes no handle and no object to the load function.

Replies are listed 'Best First'.
Re^2: Help with IO::Scalar and piping
by jakev383 (Initiate) on Sep 27, 2009 at 11:52 UTC
    Thanks. I figured I was piping it incorrectly. When you explained it I can see where I made my mistake. It is still either being interpreted incorrectly or I am really off somewhere as I cannnot get Mail::DKIM::Verifier to provide results as defined in the example. Here is my full code:
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Email::Simple; use Email::Simple::Creator; use Email::Send; use IO::Scalar; use Mail::DKIM::Verifier; use constant SENDER => 'test@circularbucket.com'; use constant SUBJECT => 'Email Message Report'; ## Main # slurp the email from STDIN my( $raw ); { local $/ = undef; local *FILE; open FILE, "-"; $raw = <FILE>; close +FILE } defined( $raw ) or die( "Nothing read from STDIN!" ); # Create an IO::Handle for Mail::DKIM::Verifier to read message from my( $raw_iohandle ) = new IO::Scalar( \$raw ); # Read from the IO::Handle my( $mail ) = Mail::DKIM::Verifier->new(); $mail->load( $raw_iohandle ); $mail->CLOSE; # Import message to Email::Simple my( $incoming ) = Email::Simple->new( \$raw ); ###################################################################### +################################## # Message checks ###################################################################### +################################## ############### # Are we spam? my( $XSpamStatus ) = 0; if ( $incoming->header( 'X-Spam-Status' ) =~ /^Yes.*score=([\S*])\s+/ +) { # we're spam, save the score $XSpamStatus = $1; } ############### # Check DKIM status my( $result ) = $mail->result; ############### # Gather who the message was from so we can send it back my( $from_header ) = $incoming->header("From"); ############################## # Gather spam stats from the message my $spam_stats1 = $incoming->header( 'X-Spam-Status' ); ############################## # Get SPF result from headers my( $SPFStatus ) = 0; if ( $incoming->header( 'Received-SPF' ) =~ /^pass.*/ ) { # we have a valid SPF record, so save the status $SPFStatus = 1; } ############################## # What MUA was used to send the message? my $user_agent = $incoming->header( 'User-Agent' ); ###################################################################### +#################################### # Generate the message to be sent back ###################################################################### +#################################### my( $body ) = <<BODY; This is a test message. The test results will follow below. ----------------------------------------------------------- BODY if ( $XSpamStatus ) { $body .= "Message was scored as spam, with a score of $XSpamStatus +\n"; } else { $body .= "Message was not scored as spam.\n"; } if ( $result ) { $body .= "Result1 was: $result\n"; } else { $body .= "Result2 was: $result\n"; } $body .= "Return path was: $from_header\n"; $body .= "Original test request was sent using: $user_agent\n"; if ( $SPFStatus ) { $body .= "\n\nSPF Record: PASS\n"; } else { $body .= "\n\nSPF Record: FAIL\n"; } foreach my $signature ($mail->signatures) { $body .= "signature identity: $signature->identity . \n"; $body .= "verify result: $signature->result_detail . \n"; } my( $outgoing ) = Email::Simple->create( header => [ From => SENDER, To => $from_header, Subject => SUBJECT, ], body => $body, ); $outgoing->header_set( 'X-Processor' => 'email test script' ); ###################################################################### +#################################### # Send the message back to the original sender ###################################################################### +#################################### my( $sender ) = Email::Send->new; $sender->send( $outgoing );
    Any nudges in the right direction are appreciated!
      • Standard input is already opened for your as STDIN, so
        # slurp the email from STDIN my( $raw ); { local $/ = undef; local *FILE; open FILE, "-"; $raw = <FILE>; close +FILE }
        is an overly complex way of doing
        # slurp the email from STDIN my $raw; { local $/; $raw = <STDIN>; }
      • Why are you forcing a list assignment?

        my( $raw_iohandle ) = new IO::Scalar( \$raw );
        should be
        my $raw_iohandle = new IO::Scalar( \$raw );

        But then again, IO::Scalar is not nearly as robust as the native support for scalar handles added to 5.8

        open(my $raw_iohandle, '<', \$raw );
      • Finally, if the module expects a system file handle (e.g. if it attempts a child process to read from it), it'll be disappointed by your attempts since none of the Perl file handles you are producing have a system file handle associated with it.

      As you don't tell where exactly you get the errors, it's kinda hard to guess what might be going wrong.

        I am no longer getting an error, but the $result printed in the $body is always "none" regardless of whether the message is signed or not, and the foreach loop never adds anything to the $body at all, which leads me to believe that the data (message) is still not being loaded into the parser correctly. Thanks.
          A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://797713]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2020-09-23 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (131 votes). Check out past polls.

    Notices?