LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   SOLUTION for extracting pdf from e-mail (https://www.linuxquestions.org/questions/programming-9/solution-for-extracting-pdf-from-e-mail-825082/)

Demerzel 08-09-2010 08:07 AM

SOLUTION for extracting pdf from e-mail
 
This can be very usefully for someone.
I was trying to extract pdf and encountered two major problems:
1. Server had Content-Transfer-Encoding: 7bit while perl MIME::Parser works with 8bit
2. When reading from file to array perl 5.8.8 drooped last 30 lines. So editing file was impossible.
This is solution:
Code:

#!/usr/bin/perl

use strict;
use warnings;
use Net::POP3;
use Mail::Header;
use MIME::Parser;

my $pop = Net::POP3->new("poczta",Timeout=>30)# conect to mail
                                or die "Can't connect to host: $!\n";
        my $messages = $pop->login("demerzeli"=>"mail")
                                or die "Can't log in: ",$pop->message,"\n";
my $last        =$pop->last();
$messages += 0;

print "inbox has $messages messages";
my $array = $pop->get(1); # write first mail to varible
$pop->quit(); #close conection
my $i=0;
foreach (@{$array}){ #varible is a pointer to array so we need to read it
       
        if($_ =~m/Content-Transfer-Encoding: 7bit/){ # MIME parser need 8bit so we will change it
                print "found";
                @{$array}[$i]="Content-Transfer-Encoding: 8bit\n";
                print $_;
        }
$i++;
}
open (OUTPUT, ">pop3.msgb");
print OUTPUT "$_" foreach (@{$array});
close(OUTPUT);

my $outputdir = "/root/testmail/";
my $parser = new MIME::Parser;
$parser->output_dir($outputdir);
        open (MAILIN, "<pop3.msgb");
        my $entity = $parser->read(\*MAILIN); # extracting mail text and attachments
        close MAILIN;


TITiAN 08-09-2010 09:41 AM

What the f***???!!! This is what I've been looking for. Thanks mate!!

BTW: If your mail login is "demerzeli", and your password "mail", I just need the hostname ("poczta" isn't valid) in order to mess with your e-Mail account.

grail 08-09-2010 10:39 AM

I must say I am a real novice when it comes to Perl, but for the life of me I cannot see what this script has to do with pdf???

I have read it a few times but I am guessing I will have to do some searching :)

Demerzel 08-09-2010 12:31 PM

This script in fact will extract any attachment. I just need pdf for my self...

TITiAN I changed login and password so don't you try to be smarter then I am. :rolleyes:

Admins could think about sticking this post somewhere to make it easy to find.


All times are GMT -5. The time now is 04:59 AM.