Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
laziness, impatience, and hubris
 
PerlMonks  

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'})?
        $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)
      {
        $attach->add($attach_file);
      }
    }
  }

  $item->Send();
  return 1;
}
Comment on Mail with Outlook
Download Code
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 encode@encode.f2s.com.
    Thx
      A good place to start is the HTML documentation provided with Perl from ActiveState. It has a section that lets you browse OLE objects.
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

    Example:
    outlook_mailer(\%mail);

    In the sub, you obtain the parameters dereferencing the reference
    my $mail_props = shift;
    and then obtain the parameters with
    $mail_props->{'to'}
    See perldoc perlref for details

    Hopes
    $_=$,=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

Back to Snippets Section

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-04-17 07:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (440 votes), past polls