linuxperlemail

Warning message from package Email::Simple


I am currently working on a project to extract information from emails on one of our servers (subject, body, attachments, etc.).

When running the code below I get the following warning :

Attempt to use reference as lvalue in substr at /usr/share/perl5/vendor_perl/Email/Simple.pm line 87.

For info, line 87 in my version of Email::Simple is

$head = substr $$text_ref, 0, $pos, '';

The warning is coming from the line of code:

my $email = Email::MIME->new(\$rawmail); 

Full code:

use strict;
use warnings;

use Net::IMAP::Simple;
use Net::IMAP::Simple::SSL;
use IO::Socket::SSL;

use Email::Simple;
use Email::MIME;

my $VERBOSE=1;
my $params;

$params->{'IMAP'}='imapAccount';
$params->{'ID'}='imapID';
$params->{'PASS'}='imapPWORD';

#Connexion au serveur imap
IO::Socket::SSL::set_defaults(SSL_verify_mode => SSL_VERIFY_NONE) ;
my $imap = Net::IMAP::Simple::SSL->new($params->{'IMAP'}) or die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n" ;
$imap->login($params->{'ID'}, $params->{'PASS'}) or die "Login failed: " . $imap->errstr . "\n";
my @search  = $imap->search_unseen ;

foreach my $imsg (@search){
    my $uid = $imap->uid($imsg) ;
    my $seq = $imap->seq($uid) ;
    print "Analyse du mail $uid - $seq - $imsg  \n"  if ($VERBOSE) ;
    my $rawmail =  $imap->get($imsg) or die "Can't get message :     $Net::IMAP::Simple::errstr\n";
    my $email = Email::MIME->new(\$rawmail);
    print "email subject:".$email->header('Subject') . "\n";
}

Info:

perl 5, version 26, subversion 3 (v5.26.3) on AlmaLinux 8.10 (Cerulean Leopard)
Email::Simple v2.216
Email::MIME v1.954

Does anyone have any idea what might be causing this warning?

Issue reported on Email::Simple.


Solution

  • Email::MIME->new is documented to take a string, so the following is the proper way to call it:

    my $email = Email::MIME->new( $rawmail );
    

    That said, Email::MIME->new also accepts a reference to a string, so the following should also work:

    my $email = Email::MIME->new( \$rawmail );
    

    The issue is that $rawmail isn't a string in your program. It looks like a string since it's an object that overloads stringification, but it isn't. And that causes problems. Fix this by forcing stringification.

    my $email = Email::MIME->new( "$rawmail" );