http://www.perlmonks.org?node_id=449873
Category: Win32 Stuff
Author/Contact Info corion-pm@corion.net
Description:

This script extracts a Lotus Notes database into separate HTML files. As I only use the Lotus Notes R4.5 client, there are many deficiencies:

  • The maximum message number is hardcoded
  • Embedded graphics get lost
  • Embedded formatting information gets lost

"Other than that", the information gets saved into an HTML::Template formatted file (see separate reply).


use strict;
use Win32::OLE qw(in);
use HTML::Template;

{
  package Wrapper::Notes::Template;
  use strict;
  use File::Spec;
  sub new {
    my ($class,$document, $attachmentdir) = @_;
    my $self = { document => $document, attachments => $attachmentdir 
+};
    bless $self, $class;
    $self;
  };
  sub document { $_[0]->{document} };
  sub param {
    my ($self,@args) = @_;
    if (scalar @args) {
      my $result;
      if ($_[1] eq 'Attachments') {
        my $result = [];

        my $body = $self->document->GetFirstItem('Body');
        my @attachments = grep { warn join ":",$_->{Name}, $_->{Type},
+$_->{Text}; $_->{Type} == 4 } (@{$self->document->Items()});
        mkdir $self->{attachments};
        for my $attname (@attachments) {
          my $url = File::Spec->catfile($self->{attachments},$attname)
+;
          $url = File::Spec->rel2abs($url);
          #warn "Extracting $attname to $url";
          my $f = $self->document->getAttachment($attname);
          if ($f) {
            $f->extractFile($url);
            push @$result, { name => $attname, url => $url };
          };
        };
        return $result;
      } elsif ($_[1] eq 'EmbeddedObjects') {
        my $result = [];

        my $body = $self->document->GetFirstItem('Body');
        my $attachments = $body->EmbeddedObjects;
        if ($attachments) {
            mkdir $self->{attachments};
            for my $att (Win32::OLE::in $attachments) {
              warn $att->{Type};
              my $url = File::Spec->catfile($self->{attachments},$att-
+>{Name});
              $url = File::Spec->rel2abs($url);
              $att->extractFile($url);
              push @$result, { name => $att->{Name}, url => $url };
            };
        };
        return $result;
      } else {
        $result = $self->document->{$_[1]};
      };
      if (ref $result) {
        return [ map { "value" => $_ }, @$result ];
      } else {
        $result;
      };
    } else {
      return (map { $_->Name } (Win32::OLE::in ($self->document->Items
+()))), "Attachments", "EmbeddedObjects";
    };
  };
};

my ($server,$database) = ('server','mail/corion.nsf');
my $Notes = Win32::OLE->new('Notes.NotesSession')
    or die "Cannot start Lotus Notes Session object.\n";
my ($Version) = ($Notes->{NotesVersion} =~ /\s*(.*\S)\s*$/);
print "The current user is $Notes->{UserName}.\n";
print "Running Notes \"$Version\" on \"$Notes->{Platform}\".\n";
my $Database = $Notes->GetDatabase($server, $database);
my $AllDocuments = $Database->AllDocuments;
my $Count = $AllDocuments->Count;
print "There are $Count documents in the database.\n";
my $Index = 4419;
while (++$Index <= $Count) {
    my $Document = $AllDocuments->GetNthDocument($Index);
    my $wrapper = Wrapper::Notes::Template->new($Document,sprintf "ema
+il/mail.%05g",$Index);
    my $template = HTML::Template->new(
                        filename => 'lotus-email.tmpl',
                        die_on_bad_params => 0,
                        loop_context_vars => 1,
                        associate => [ $wrapper ],
                        case_sensitive => 1,
                        );
    my $outfile = sprintf "email/mail.%05g.html", $Index;
    open MAIL, ">", $outfile or die "Couldn't create '$outfile' : $!\n
+";
    $template->output( print_to => *MAIL );
    close MAIL;
    last unless $Index <= 4420; # magic number!
}