#!/cygdrive/c/Perl/bin/perl.exe -w #Win32 perl script to send an email and (optional) attachments via Mic +rosoft Outlook use warnings; use strict; use Win32::OLE; #get parameters from command line #e.g. send_outlook.pl spam_me@hotmail.com "get rich quick!" "send me m +oney and I make you rich!" "1337spammer@hotmail.com" "c:\melissa.exe" #first (and only required param) is "To:" address #second is subject #third is text for message body #fourth is CC: address (you can leave this blank, e.g "" on the comma +nd line) #rest are file attachments #make sure you give the FULL path to the attachment or it may fail my $to = shift || die "required parameter (to address) missing"; my $subject = shift; $subject = "" if not defined $subject; my $body = shift; $body = "" if not defined $body; my $cc = shift; $cc = "" if not defined $cc; #get new Outlook instance my $mail = new Win32::OLE('Outlook.Application'); die "Unable to start Outlook instance: $!" if !defined $mail; my $item = $mail->CreateItem(0); die "Unable to create mail item: $!" if !defined $item; $item->{'To'} = $to; $item->{'CC'} = $cc; $item->{'Subject'} = $subject; $item->{'Body'} = $body; #rest of args are file attachments foreach my $attach (@ARGV) { #make sure the attachment is really there die "Missing attachment $attach: $!" if !-e $attach; my $attachments = $item->Attachments(); $attachments->Add($attach); } #send it $item->Send(); my $error = Win32::OLE->LastError(); print STDERR "Win32::OLE error: $error" if $error;

In reply to Send email (and attachments) with Outlook by RhetTbull

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.