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

I have a very short script to send myself a sample mail. I get the error message,
Can't call method "Send" on unblessed reference at C:\testmail.pl line + 14. The script is: #!/usr/local/bin/perl use Win32::OLE; # Send report by mail $outlook = new Win32::OLE('Outlook.Application'); $outlook->{Visible}=1; $mailitem = $outlook->CreateItem(olMailItem); $mailitem->{To} = "hrobertson\@deltagen.com"; $mailitem->{Subject} = "Copy report for $d"; open(INFILE,"copylog.txt"); $mailitem->{Body} .= <INFILE>; close(INFILE); $mailitem->Send(); ---- What do I need to do differently??

Replies are listed 'Best First'.
Re: trouble sending mail
by Adam (Vicar) on Mar 22, 2001 at 05:34 UTC
    I think there might be a problem with your CreateItem() method failing to return an object. Since you don't have strict turned on (shame on you!) you didn't find out that $mailitem is not defined. Then you use it as a hash ref, which Perl is happy to do for you. And then you try to call send on a hash ref, not an olMailItem (or whatever).

    So the simple answer is, use strict; and check your Ole calls with or die. Be sure to read up on Ole mechanisms and the COM Interface to Outlook. The better answer is open Outlook and figure out where the mail server is. Then use Net::SMTP or MIME::Lite or the like. Doing it this way cuts out the overhead of loading Outlook and eliminates your dependence on both MS and Outlook being installed.

    Update:
    dws caught the source of your Ole error, the constant, but he didn't give you the right answer for what to do about it. You should import the constants via Ole:

    use Win32::OLE::Const; my %olConst = %{ Win32::OLE::Const->Load( "Microsoft Outlook" ) };
    Then you can just get them out of that hash table.
Re: trouble sending mail
by dws (Chancellor) on Mar 22, 2001 at 05:32 UTC
        $mailitem = $outlook->CreateItem{olMailItem); is failing (it returns undef). Then, instead of tickling an OLE object,     $mailitem->{To} = "..."; creates a new hash instead, which you discover later when you try     $mailitem->Send(); The underlying problem is that you haven't defined olMailItem. You can fix this* by adding     use constant olMailItem => 0; to the top of your script.

    You would have discovered this yourself had you checked for undef after each attempt to create a new OLE object. Blindly assuming that each OLE object creation succeeds is not a good strategy.

    * or by following Adam's advice below. His is the more robust advice, though several of Microsoft's own books (e.g., Building Applications with Outlook 97) use the magic constant 0 in this case.

Re: trouble sending mail
by dorpus (Novice) on Mar 23, 2001 at 03:59 UTC
    ok I got it to work now with:
    #!/usr/local/bin/perl use strict; use Win32::OLE::Const; my %olConst = %{ Win32::OLE::Const->Load( "Microsoft Outlook" ) }; # Send report by mail my $outlook = new Win32::OLE('Outlook.Application'); my $mailitem = $outlook->CreateItem($olConst{olMailItem}) or die "coul +d not create item."; $mailitem->{To} = "spam@spam.com"; $mailitem->{Subject} = "Copy report"; $mailitem->{Body} .= "abc"; $mailitem->Send();
    Thanks
      The risk of actively exposing your email address in a script is that you're now liable to start receiving "Copy report" emails from all over the world as people try out your script. :)

      You might counter by keeping a list of senders on your homenode.

Re: trouble sending mail
by Beatnik (Parson) on Mar 23, 2001 at 05:00 UTC
    As a sidenote, NT Sendmail, and a handfull of other apps can be used to use CGIs to send mail on Win32 platforms.

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.