Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Email on Win32, MSExchange

by Zo (Scribe)
on Mar 06, 2002 at 16:44 UTC ( [id://149737]=perlquestion: print w/replies, xml ) Need Help??

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

This is probably a very simple question or problem, but so far I've been going well and I need some extra eyes to guide me. I get an error at the 'Logon'. Here is the posted code.
Perl 5.06.., on Win2000 Server

#!perl use Win32::OLE; use OLE; my $MAPISession = CreateObject OLE "MAPI.Session" || die; $MAPISession->Logon('servername','password'); #### ####this is where it fails ^ #### $newfile = "./perl_test.txt"; open(BATCH, ">>$newfile") || die "Can't open $newfile : the perl_test. +txt file. $!"; ################## sub send_outlook_mail { #use OLE and Outlook to send an email message my $to = ("me\@myemail.com"); my $subject ="DB connection failure"; # my $body = "The connection failed.\n"; # my $cc = shift; # my $cc = ("you\@youremail.com"); #get new Outlook instance my $mail = new Win32::OLE('Outlook.Application') || die "Unable to + start Outlook instance: $!"; my $item = $mail->CreateItem(0) || die "Unable to create mail item +: $!"; $item->{'To'} = $to; # $item->{'CC'} = $cc; $item->{'Subject'} = $subject; # $item->{'Body'} = $body; #rest of args are file attachments foreach my $attach (@ARGV) { # print STDERR "File attachment: $attach\n"; #make sure the attachment is really there # croak "Missing attachment $attach: $!" if !-e $attach; # my $attachments = $item->Attachments(); # $attachments->Add(); } $item->Send(); my $error = Win32::OLE->LastError(); #carp "Win32::OLE error: $error" if $error; } #sub send_outlook_mail ################ $time = `Time /T`; print BATCH $xx." ".$time; send_outlook_mail(); $MAPISession->Logoff(); # close new .txt file close (BATCH);

Any help? Is this something simple I'm overlooking? I have sent email from my desktop with the working function in my code 'send_outlook_mail', but was trying to do it from one of our servers. So I went to www.deja.com and other sites to get help/information. I took the info from the MAPI connection and worked it around my send_outlook_mail function, knowing that the email sending works on my pc here. Thought if I made the connection on the server via MAPI in windows this could work. Is my thought on this and code way off?
Thank you all in advance for any/all help.
.Zo

Replies are listed 'Best First'.
Re: Email on Win32, MSExchange
by gav^ (Curate) on Mar 06, 2002 at 17:15 UTC
    Why would you want to complicate things when sending over SMTP is so easy and works with Exchange?

    gav^

Re: Email on Win32, MSExchange
by strat (Canon) on Mar 06, 2002 at 17:12 UTC
    Maybe if you ask for an error message and die with it, it could help finding the problem:

    ActivePerl-WinFaq12 tells us:
    Why doesn't $! get set with the error message I am generating?
    Error messages from Win32::OLE doesn't go to the $! variable, but can be accessed as Win32::OLE->LastError()

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

Re: Email on Win32, MSExchange
by grummerX (Pilgrim) on Mar 06, 2002 at 18:26 UTC
    I notice that you're passing servername rather than ProfileName to the Logon function, when according to my handy ActiveState Win32::OLE Type Library Browser the Logon function for MAPI.Session looks like:
    Function Logon([ProfileName], [ProfilePassword], [ShowDialog], [NewSession], [ParentWindow], [NoMail], [ProfileInfo])
    I have a mail filter script that uses the following code (which seems to work with your code as well):
    $session = Win32::OLE->new("MAPI.Session") or die "Can't establish session : ".Win32::OLE->LastError(); $session->Logon(undef, undef, 0, 0);
    You may have to actually provide values for ProfileName and ProfilePassword depending on how the defaults are set on your system.

    -- grummerX

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://149737]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-24 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found