Perl
Home
Perl
  Regular Expressions
  Escape Sequences
  OLE - Outlook
  OLE - Excel
  OLE - Word
  Links
SQL
Services
Links
OLE - Outlook
Send Email With Attachments

use Win32::OLE;
use Win32::OLE qw(in with);
use Win32::OLE::Variant;
use Win32::OLE::Const 'Microsoft Outlook';

my $Outlook = Win32::OLE->GetActiveObject
                           ('Outlook.Application') ||
                Win32::OLE->new('Outlook.Application');
my $Outlook = new Win32::OLE('Outlook.Application');

# Create Mail Item
my $item = $Outlook->CreateItem(0);  # 0 = mail item.

unless ($item)
{
  die "Outlook is not running, cannot send mail.\n";
}

$item->{'Subject'} = $mail_props{'subject'} ||
                     '[No Subject]';
$item->{'To'} = join
              (";", split(/[ ,;]+/, $mail_props{'to'}));
$item->{'Body'} = $mail_props{'body'} || "\r\n";

$item->{'From'} = $mail_props{'from'}
       if (exists $mail_props{'from'});
$item->{'Cc'} = $mail_props{'cc'}
       if (exists $mail_props{'cc'});
$item->{'Bcc'} = $mail_props{'bcc'}
       if (exists $mail_props{'bcc'});

# 2=high, 1=normal, 0=low
$item->{'Importance'} =(exists $mail_props{'importance'})?
      $mail_props{'importance'}: 1;

# Add Attachemnts
if (exists $mail_props{'attachments'})
{
   $attach = $item->{'Attachments'};

  foreach my $attach_file (@mail_props{'attachments'})
  {
    if ($attach_file and -f $attach_file)
    {
      $attach->add($attach_file);
    }
  }
}

# Send the Email
$item->Send();