Voilà des jours que je bloque sur Perl. Je suis un piètre programmeur et je me suis mis à Perl voilà 2 semaines, et je galère comme pas possible.
Mon script doit extraire la pièce jointe du dernier mail arrivé dans une boite IMAP.
Je dois pas en être loin, puisque mon script arrive à afficher le contenu du message grâce au module IMAP:Talk.
Le problème, c'est qu'ensuite j'essaye de passer le message au module Attachment::stripper. Celui ci attend un objet MIME et apparemment c'est cette transmission que j'arrive pas à faire...
Voici mon code :
#!/usr/bin/perl -w
use Email::Simple;
use Email::MIME;
use Mail::IMAPTalk;
use Email::MIME::Attachment::Stripper ;
$FolderName = 'INBOX';
$imap = Mail::IMAPTalk->new(
Server => '192.168.0.1',
Port => 143,
Username => 'user',
Password => 'pass',
Separator => '.',
RootFolder => 'Inbox',
CaseInsensitive => 1)
|| die "Connection failed. Reason: $@";
# Select folder and get first unseen message
$imap->select($FolderName) || die $@;
$imap->select($FolderName) || die $@;
$MsgId = $imap->search('not', 'seen')->[0];
print "Message ID :".$MsgId."\n";
print "Message ID2 :".$imap->search('not', 'seen')->[0]."\n";
print "quoi :".$imap->search('not', 'seen')->[1]."\n";
$MsgEV = $imap->fetch($MsgId, 'envelope')->{$MsgId}->{envelope};
print "From: " . $MsgEV->{From}."\n";
print "To: " . $MsgEV->{To}."\n";
print "Subject: " . $MsgEV->{Subject}."\n";
print "Sender: " . $MsgEV->{Sender}."\n";
# Get message body structur
my $MsgBS = $imap->fetch($MsgId, 'bodystructure')->{$MsgId}->{bodystructure};
print "Size: ".$MsgBS->{Size}."\n";
# Find imap part number of text part of message
$MsgTxtHash = Mail::IMAPTalk::find_message($MsgBS);
$MsgPart = $MsgTxtHash->{text}->{'IMAP-Partnum'};
# Retrieve message text body
$MsgTxt = $imap->fetch($MsgId, "body[$MsgPart]")->{$MsgId}->{body};
print "Content: ".$MsgTxt."\n";
my $parsed = Email::MIME->new($MsgPart);
print "parsed ".$parsed."\n";
print "structure :".$parsed->debug_structure."\n";
my $parts = $parsed->parts;
print "nb parts ".$parts."\n";
my @parts = $parsed->parts;
my $i=0;
foreach (@parts) {
my $j=0;
foreach ($parts[$i]) {
print "parts ".$parts[$i]."\n";
$j++;
}
$i++;
}
my $decoded = $parsed->body;
print "body ".$decoded."\n";
my $stripper = Email::MIME::Attachment::Stripper->new($parsed);
my Email::MIME $msg2 = $stripper->message;
print "message ".$msg2[0]."\n";
my @attachments = $stripper->attachments;
my $i=0;
foreach (@parts) {
my $j=0;
foreach ($parts[$i]) {
print "hash :".$attachments[$i]->{payload}."\n";
$j++;
}
$i++;
}
Si une âme charitable veut bien m'aider...
Merci d'avance à tous ceux qui vont regarder mon code.
# @attachments
Posté par Joris Dedieu (site web personnel) . Évalué à 4.
[^] # Re: @attachments
Posté par Anonyme . Évalué à 2.
ça m'a permit de réaliser que IMAP::Talk renvoie une structure alors que Attachment::Stripper attends une string.
Par contre j'ai pas encore trouvé comment récupérer une string du coup...
Voilà aussi mon code un peu nettoyé :
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use Email::Simple;
use Email::MIME;
use Mail::IMAPTalk;
use Email::MIME::Attachment::Stripper ;
use constant DUMP => '/home/jc';
my $server = '192.168.1.116';
my $imap_port = '143';
my $login = 'jc';
my $password = 'pass';
my $FolderName = 'INBOX';
print "******************\n";
print "* MAIL EXTRACT *\n";
print "******************\n\n";
print "Connecting to IMAP server at ".$server.":".$imap_port."...\n";
# open the imap connection using IMAP::Talk
my $imap = Mail::IMAPTalk->new(
Server => $server,
Port => $imap_port,
Username => $login,
Password => $password,
Separator => '.',
RootFolder => 'Inbox',
CaseInsensitive => 1)
|| die "Connection failed. Reason: $@";
# Select folder and get first unseen message
$imap->select($FolderName) || die $@;
my $MsgId = $imap->search('not', 'seen')->[0];
if ($MsgId) {
# Get the enveloppe
print "The message with ID ".$MsgId." has the following enveloppe :\n";
my $MsgEV = $imap->fetch($MsgId, 'envelope')->{$MsgId}->{envelope};
print "From: " . $MsgEV->{From}."\n";
print "To: " . $MsgEV->{To}."\n";
print "Subject: " . $MsgEV->{Subject}."\n";
print "Sender: " . $MsgEV->{Sender}."\n";
print "Continue ?\n";
getc(STDIN);
# Get the message body structure
my $MsgBS = $imap->fetch($MsgId,'bodystructure')->{$MsgId}->{bodystructure};
print "The size of the message is ".$MsgBS->{Size}."kB\n";
print "Continue ?\n";
getc(STDIN);
# Find imap part number of text part of message
my $MsgTxtHash = Mail::IMAPTalk::find_message($MsgBS);
my $MsgPart = $MsgTxtHash->{text}->{'IMAP-Partnum'};
# Retrieve message text body
my $MsgTxt = $imap->fetch($MsgId, "body[$MsgPart]")->{$MsgId}->{body};
print "Full message : \n";
print "\n<-------------------------------------->\n";
print $MsgTxt."\n";
print "\n<-------------------------------------->\n";
print "Continue ?\n";
getc(STDIN);
# Transform the content from IMAP:Talk into a MIME object using Email:MIME
my $parsed = Email::MIME->new($MsgTxt);
print "Parsed content :\n". Dumper( $parsed) . "\n";
# display the MIME structure
print "MIME structure :" . $parsed->debug_structure . "\n";
my $parts = $parsed->parts;
print "Number of email parts : $parts\n";
my @parts = $parsed->parts;
my $i=0;
#foreach (@parts) {
# print "Dumped email part $i:\n",
# Dumper( $parts[$i] ),
# "\n";
# $i++;
#}
my $decoded = $parsed->body;
#print "body: " . Dumper( $decoded ) . "\n";
# Give the Email MIME content to Attachment::Stripper for extraction
my $stripper;
if ($parts > 1) {
$stripper = Email::MIME::Attachment::Stripper->new($parts[1]);
}
else {
die "This message consists of a single part.\n";
}
die "Not an regular MIME part given to stripper:\n"
if not ref $stripper;
# The extraction method itself
my @attachments = $stripper->attachments;
# Display the resulting attachments hash
$i=0;
foreach (@parts) {
my $j=0;
foreach ($parts[$i]) {
print "hash:\n" . Dumper( $attachments[$i]->{payload}) . "\n";
$j++;
}
$i++;
}
# Close the IMAP connection
$imap->logout();
# Save the attachments on the local disk
foreach $i (@attachments)
{
my $file = 'test'.$i; #DUMP . @attachments->filename(1);
open FILE, '>', $file or die $!;
print FILE @attachments;
close FILE;
chmod 0644, $file;
}
}
else {
print "No new message in the mailbox\n"
}
Suivre le flux des commentaires
Note : les commentaires appartiennent à celles et ceux qui les ont postés. Nous n’en sommes pas responsables.