Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do you add multiple attachments? I cant seem to make it work. It only attaches the last file. Thanks in advance. Heres my code:
#!/usr/bin/perl use Win32::OLE; use Win32::OLE::Const; use Win32::OLE::Const 'Microsoft Outlook'; # #Send email using outlook @attach = ("H:\\Scripts\Chandras reports\\Limit Increase(M) 072005.xls +", "H:\\Scripts\\Chandras reports\\Limit Increase(D) 072005.xls"); $mail = new Win32::OLE('Outlook.Application'); $safe = new Win32::OLE('Redemption.SafeMailItem'); # #$mail->{'Visible'} = 1; $item = $mail->CreateItem(0); $item->{'Subject'} = "Credit Limit Increase (D & M)"; $item->{'To'} = "me\@my.com"; $item->{'Body'} = "Tom,\nhere are the Credit Limit Reports."; #my $attachments = $item->Attachments(); #$attachments->Add(@attach); foreach my $attach (@attach) { # Verify the existence of the file attachment next unless ( -e $attach ); # Go ahead and attach this file my $Attachments = $item->Attachments(); $Attachments->Add($attach); } $item->Send(); $item->Quit();

Replies are listed 'Best First'.
Re: Multiple attachments Outlook
by idsfa (Vicar) on Aug 25, 2005 at 17:12 UTC

    Why on earth would you do this with OLE? You are invoking a sledgehammer when you need a flyswatter.

    I would recommend MIME::Lite for this task.

    use MIME::Lite; ### Create a new multipart message: $msg = MIME::Lite->new( From =>'autoscript@my.com', To =>'me@my.com', Subject =>'Credit Limit Increase (D & M)', Type =>'multipart/mixed' ); ### Add parts $msg->attach(Type =>'TEXT', Data =>"Tom,\nhere are the Credit Limit Reports." ); foreach my $attach (@attach) { next unless ( -e $attach); $msg->attach(Type =>'application/vnd.ms-excel', Path => $attach, Filename => $attach, Disposition => 'attachment' ); } MIME::Lite->send('smtp', "smtp.my.com", Timeout=>60); $msg->send;

    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. -- Cyrus H. Gordon
      I cant send it through SMTP, I do not have access. Thanks though...
Re: Multiple attachments Outlook
by InfiniteSilence (Curate) on Aug 25, 2005 at 17:37 UTC
    I think you messed up with the scope. Shouldn't it be:
    my $Attachments = $item->Attachments(); foreach my $attach(@attach) { $Attachments->Add($attach); }
    ???

    Celebrate Intellectual Diversity

      It still does the same thing even with the change in code with "my $Attachments = $item->Attachments();" outside of the foreach loo"p. It only attaches one file. Thanks for your help.
        Did you debug the program and step through the code where you are checking to see if the file is there (-e)? You may only be getting one file because it may not be firing that code. Try that.

        Celebrate Intellectual Diversity

Re: Multiple attachments Outlook
by ftq (Initiate) on Aug 27, 2005 at 03:03 UTC
    You should get into the habit of using '/' forward slashes when ever you code even in windows...

    Your error is here...

    @attach = ( "H:\\Scripts\Chandras reports\\Limit Increase(M) 072005.xls", # ----------^, should be '\\' "H:\\Scripts\\Chandras reports\\Limit Increase(D) 072005.xls" );
    Although there is nothing wrong with you code, I would write it different. If you can send via Out Look, you can send via SMTP or HTTP through Perl. Also note, any Personal type OLE object (office) created in Perl is not always released when the your script exists.

    Sonia
      Thanks guys for all ur help.. I really appreciate it. I got it to finally work with everyones help.