Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Mail with Outlook

by jgamble (Pilgrim)
on Dec 02, 2001 at 07:34 UTC ( #128957=snippet: print w/replies, xml ) Need Help??
Description: Thanks to the code written in various Q&A sections, i wrote my own function to send mail using Outlook. This function uses Win32::OLE (of course), and returns 0 on failure and 1 for success. It does *not* open or close Outlook, which was outside the scope of the function - you can open or close it yourself in the calling code. I'll follow up with a similar function that uses the CDONTS lib instead of Outlook.
sub outlook_mailer
  my(%mail_props) = @_;

  unless (exists $mail_props{'to'} and defined $mail_props{'to'})
    carp "No Addressee to send mail to.\n";
    return 0;

  my $mail = new Win32::OLE('Outlook.Application');
  my $item = $mail->CreateItem(0);  # 0 = mail item.

  unless ($item)
    carp "Outlook is not running, cannot send mail.\n";
    return 0;

  $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'});

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

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

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

  return 1;
Replies are listed 'Best First'.
Re: Mail with Outlook
by hopes (Friar) on Dec 13, 2001 at 15:30 UTC
    Only one comment:
    The body should be large, and you are passing to the function (using the stack) all the parameters.
    I would pass it using references


    In the sub, you obtain the parameters dereferencing the reference
    my $mail_props = shift;
    and then obtain the parameters with
    See perldoc perlref for details

    $_=$,=q,\,@4O,,s,^$,$\,,s,s,^,b9,s, $_^=q,$\^-]!,,print
      Yup, you're right, i should have written this using a reference for the $mail_props variable. Thanks. -john
Re: Mail with Outlook
by encode (Initiate) on Dec 13, 2001 at 11:01 UTC
    You seem to be reasonably experienced with Win32::OLE. I've just started looking at it. If anyone has used it to create new users' mailboxes in Exchange 2000 on WinNT, i'd very much like to hear from you.
    You can email me at
      A good place to start is the HTML documentation provided with Perl from ActiveState. It has a section that lets you browse OLE objects.
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://128957]
[1nickt]: Discipulus Yes, I think it is maybe easier to be a good monk in "real" life when you are young and idealistic or old and weary ... nost so much in middle age.
[1nickt]: 😲 ... or did you mean the Middle Ages, when monastic life was more, umm, "earthy" ?!

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2017-12-13 14:05 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (368 votes). Check out past polls.