Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Subclassing Log::Dispatch::Email

by dorko (Prior)
on Sep 25, 2009 at 15:14 UTC ( [id://797537]=perlquestion: print w/replies, xml ) Need Help??

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

Has anyone subclassed Log::Dispatch::Email for use with Email::Stuff and have some code they're willing to share? It doesn't look hard, but I've never done it before and I'm not sure I'll get it right on my first couple of tries.

Thanks for your consideration.

Cheers,

Brent

-- Yeah, I'm a Delt.

Replies are listed 'Best First'.
Re: Subclassing Log::Dispatch::Email
by Bloodnok (Vicar) on Sep 25, 2009 at 15:43 UTC
    I'd suggest you JFDI and come back if you hit any probs.

    A user level that continues to overstate my experience :-))
      Thanks for the advise. It's a project I'm working on for a small charity in my spare time, so I didn't feel too bad about asking if anyone wanted to share their existing code. In the spirit in which I asked the question, here's what I wrote:
      package Log::Dispatch::Email::Emailstuff; use strict; use warnings; use Log::Dispatch::Email; use base qw( Log::Dispatch::Email ); use Email::Stuff; sub send_email { my $self = shift; my %p = @_; Email::Stuff->to (join ',', @{$self->{to}}) ->from ($self->{from}) ->subject ($self->{subject}) ->text_body ($p{message}) # ->attach_file ('choochoo.gif') ->using ('SMTP', Host => 'smtp.server.goes.her +e.com') ->send; } 1; __END__ =head1 NAME Log::Dispatch::Email::Emailstuff - Subclass of Log::Dispatch::Email th +at uses the Email::Stuff module =head1 SYNOPSIS use Log::Dispatch::Email::Emailstuff; my $email = Log::Dispatch::Email::Emailstuff->new ( name => 'email', min_level => 'emerg', to => [ qw( foo@bar.com bar@baz.org ) ], subject => 'Oh no!!!!!!!!!!!', ); $email->log( message => 'Something bad is happening', level => 'emer +g' ); =head1 DESCRIPTION This is a subclass of Log::Dispatch::Email that implements the send_email method using the Email::Stuff module. =head1 METHODS =over 4 =item * new This method takes a hash of parameters. The following options are valid: =over 8 =item * name ($) The name of the object (not the filename!). Required. =item * min_level ($) The minimum logging level this object will accept. See the Log::Dispatch documentation on L<Log Levels|Log::Dispatch/"Log Levels" +> for more information. Required. =item * max_level ($) The maximum logging level this obejct will accept. See the Log::Dispatch documentation on L<Log Levels|Log::Dispatch/"Log Levels" +> for more information. This is not required. By default the maximum is the highest possible level (which means functionally that the object has no maximum). =item * subject ($) The subject of the email messages which are sent. Defaults to "$0: log email" =item * to ($ or \@) Either a string or a list reference of strings containing email addresses. Required. =item * from ($) A string containing an email address. This is optional and may not work with all mail sending methods. This option works with Email::Stuff. NOTE: The Mail::Sendmail module requires an address be passed to it to set this in the mail it sends. We pass in 'LogDispatch@foo.bar' as the default. =item * using Specifies the Email::Send driver that you want to use to send the emai +l, and any options that need to be passed to the driver at the time that we send the mail. Alternatively, you can pass a complete mailer object (which must be an Email::Send object) and it will be used as is. =item * buffered (0 or 1) This determines whether the object sends one email per message it is given or whether it stores them up and sends them all at once. The default is to buffer messages. =item * callbacks( \& or [ \&, \&, ... ] ) This parameter may be a single subroutine reference or an array reference of subroutine references. These callbacks will be called in the order they are given and passed a hash containing the following ke +ys: ( message => $log_message, level => $log_level ) The callbacks are expected to modify the message and then return a single scalar containing that modified message. These callbacks will be called when either the C<log> or C<log_to> methods are called and will only be applied to a given message once. =back =item * log_message( level => $, message => $ ) Sends a message if the level is greater than or equal to the object's minimum level. =back =head1 AUTHOR Dave Rolsky, <autarch@urth.org> wrote Log::Dispatch::Email and Brent Shaw, <brent.shaw.ucf@gmail.com> made it work with Email::Stuff =cut

      Cheers,

      Brent

      -- Yeah, I'm a Delt.

Log In?
Username:
Password:

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

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

    No recent polls found