Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Send email (and attachments) with Outlook

by RhetTbull (Curate)
on Sep 21, 2001 at 15:30 UTC ( #113851=sourcecode: print w/ replies, xml ) Need Help??

Category: Win32
Author/Contact Info Rhet Turnbull
RhetTbull at hotmail.com
Description: Send an email and, optionally, attachments using Win32::OLE and Microsoft Outlook.
#!/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;

Comment on Send email (and attachments) with Outlook
Download Code

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://113851]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-09-19 22:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (149 votes), past polls