home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / MailSend.pm < prev    next >
Encoding:
Perl POD Document  |  2003-09-27  |  3.8 KB  |  165 lines

  1. package Log::Dispatch::Email::MailSend;
  2.  
  3. use strict;
  4.  
  5. use Log::Dispatch::Email;
  6.  
  7. use base qw( Log::Dispatch::Email );
  8.  
  9. use Carp ();
  10. use Mail::Send;
  11.  
  12. use vars qw[ $VERSION ];
  13.  
  14. $VERSION = sprintf "%d.%02d", q$Revision: 1.19 $ =~ /: (\d+)\.(\d+)/;
  15.  
  16. 1;
  17.  
  18. sub send_email
  19. {
  20.     my $self = shift;
  21.     my %p = @_;
  22.  
  23.     my $msg = Mail::Send->new;
  24.  
  25.     $msg->to( join ',', @{ $self->{to} } );
  26.     $msg->subject( $self->{subject} );
  27.  
  28.     # Does this ever work for this module?
  29.     $msg->set('From', $self->{from}) if $self->{from};
  30.  
  31.     eval
  32.     {
  33.     my $fh = $msg->open
  34.         or die "Cannot open handle to mail program";
  35.  
  36.     $fh->print( $p{message} )
  37.         or die "Cannot print message to mail program handle";
  38.  
  39.     $fh->close
  40.         or die "Cannot close handle to mail program";
  41.     };
  42.  
  43.     warn $@ if $@ && $^W;
  44. }
  45.  
  46.  
  47. __END__
  48.  
  49. =head1 NAME
  50.  
  51. Log::Dispatch::Email::MailSend - Subclass of Log::Dispatch::Email that uses the Mail::Send module
  52.  
  53. =head1 SYNOPSIS
  54.  
  55.   use Log::Dispatch::Email::MailSend;
  56.  
  57.   my $email =
  58.       Log::Dispatch::Email::MailSend->new
  59.           ( name => 'email',
  60.             min_level => 'emerg',
  61.             to => [ qw( foo@bar.com bar@baz.org ) ],
  62.             subject => 'Oh no!!!!!!!!!!!', );
  63.  
  64.   $email->log( message => 'Something bad is happening', level => 'emerg' );
  65.  
  66. =head1 DESCRIPTION
  67.  
  68. This is a subclass of Log::Dispatch::Email that implements the
  69. send_email method using the Mail::Send module.
  70.  
  71. =head1 METHODS
  72.  
  73. =over 4
  74.  
  75. =item * new
  76.  
  77. This method takes a hash of parameters.  The following options are
  78. valid:
  79.  
  80. =over 8
  81.  
  82. =item * name ($)
  83.  
  84. The name of the object (not the filename!).  Required.
  85.  
  86. =item * min_level ($)
  87.  
  88. The minimum logging level this object will accept.  See the
  89. Log::Dispatch documentation for more information.  Required.
  90.  
  91. =item * max_level ($)
  92.  
  93. The maximum logging level this obejct will accept.  See the
  94. Log::Dispatch documentation for more information.  This is not
  95. required.  By default the maximum is the highest possible level (which
  96. means functionally that the object has no maximum).
  97.  
  98. =item * subject ($)
  99.  
  100. The subject of the email messages which are sent.  Defaults to "$0:
  101. log email"
  102.  
  103. =item * to ($ or \@)
  104.  
  105. Either a string or a list reference of strings containing email
  106. addresses.  Required.
  107.  
  108. =item * from ($)
  109.  
  110. A string containing an email address.  This is optional and may not
  111. work with all mail sending methods.
  112.  
  113. =item * buffered (0 or 1)
  114.  
  115. This determines whether the object sends one email per message it is
  116. given or whether it stores them up and sends them all at once.  The
  117. default is to buffer messages.
  118.  
  119. =item * callbacks( \& or [ \&, \&, ... ] )
  120.  
  121. This parameter may be a single subroutine reference or an array
  122. reference of subroutine references.  These callbacks will be called in
  123. the order they are given and passed a hash containing the following keys:
  124.  
  125.  ( message => $log_message, level => $log_level )
  126.  
  127. The callbacks are expected to modify the message and then return a
  128. single scalar containing that modified message.  These callbacks will
  129. be called when either the C<log> or C<log_to> methods are called and
  130. will only be applied to a given message once.
  131.  
  132. =back
  133.  
  134. =item * log_message( level => $, message => $ )
  135.  
  136. Sends a message if the level is greater than or equal to the object's
  137. minimum level.
  138.  
  139. =back
  140.  
  141. =head1 CHANGING HOW MAIL IS SENT
  142.  
  143. Since C<Mail::Send> is a subclass of C<Mail::Mailer>, you can change
  144. how mail is sent from this module by simply C<use>ing C<Mail::Mailer>
  145. in your code before mail is sent.  For example, to send mail via smtp,
  146. you could do:
  147.  
  148.   use Mail::Mailer 'smtp', Server => 'foo.example.com';
  149.  
  150. For more details, see the C<Mail::Mailer> docs.
  151.  
  152. =head1 AUTHOR
  153.  
  154. Dave Rolsky, <autarch@urth.org>
  155.  
  156. =head1 SEE ALSO
  157.  
  158.  
  159. Log::Dispatch, Log::Dispatch::ApacheLog, Log::Dispatch::Email,
  160. Log::Dispatch::Email::MailSendmail, Log::Dispatch::Email::MIMELite,
  161. Log::Dispatch::File, Log::Dispatch::Handle, Log::Dispatch::Output,
  162. Log::Dispatch::Screen, Log::Dispatch::Syslog
  163.  
  164. =cut
  165.