i have written a script that saves the attachments from all unread messages from a gmail account to my hard drive I am doing this by printing the source code of the attachments into a pdf file created during the scripts run. But all of the PDF files are almost completely corrupt (for some reason 1 in around 20 pages in each file are fine the rest are completely blank) Is there another way to do this or a way to programmatically fix the pdf files I have been at this for days and I i still haven't figured out so any help is appreciated thanks in advance.

use strict; use warnings; # required modules use Net::IMAP::Simple; use Email::MIME; use IO::Socket::SSL; use Email::MIME::Attachment::Stripper; # fill in your details here my $username = 'username@website.com'; my $password = 'password'; my $mailhost = 'imap.gmail.com'; # Connect my $imap = Net::IMAP::Simple->new( $mailhost, port => 993, use_ssl => 1, ) || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n"; # Log in if ( !$imap->login( $username, $password ) ) { print STDERR "Login failed: " . $imap->errstr . "\n"; exit(64); } # Look in the the INBOX my $nm = $imap->select('INBOX'); # How many messages are there? my ($unseen, $recent, $num_messages) = $imap->status(); print "unseen: $unseen, recent: $recent, total: $num_messages\n\n"; my $filepath = "if you want to test it insert own file path here"; ## Iterate through unseen messages for ( my $i = 1 ; $i <= $nm ; $i++ ) { if (!$imap->seen($i) ) { next; } else { my $es = Email::MIME->new( join '', @{ $imap->get($i) } ); #my $es = Email::MIME->new( join '', @{ $imap->top($i) } ); my $text = $es->body; my $stripper = Email::MIME::Attachment::Stripper->new($es); my @attachments = $stripper->attachments; printf( "[%03d] %s\n\t%s\n%s", $i, $es->header('From'), $es->heade +r('Subject'),$text); my $l = 0; foreach $_(@attachments) { my $fh = IO::File->new(); open($fh, '>', "$filepath" . "$_->{filename}"); print $fh "$_->{payload}\n"; $fh->close; } } } # Disconnect $imap->quit; exit;

In reply to Saving PDF files from gmail by anonymonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.