package RWDE::Mailing;

use strict;
use warnings;

use vars qw($VERSION);
$VERSION = sprintf "%d", q$Revision: 507 $ =~ /(\d+)/;


=head1 RWDE::Mailing

Mailing interface for objects that can receive emails.

Throttling is also provided to keeping daily count of emails sent under control. 

To be clear this is meant to be used as a base class and derived classes are required to 
implement certain methods so the facility works correctly.

=head2 get_email()

Retrieve the class defined email address. This is specifically assigned within the RWDE
Object hash for the purpose of dispatching emails.


sub get_email {
  my ($self, $params) = @_;

  my $email_field = $self->{_email};

  throw RWDE::DevelException({ info => " $self does not have an email address" })
    unless defined $email_field;

  return $self->$email_field;

=head2 send_message()

This method is required to be implemented by your derived class. If you don't implement it
an DevelException will be thrown upon invocation.

The implementation for this method should include a call to _send_message in order for the interface
to work end to end.


sub send_message {
  my ($self, $params) = @_;

  my $class = ref $self || $self;
  throw RWDE::DevelException({ info =>"Class $class does not implement send_message" });

=head2 _send_message()

Verify that that record has no exceeded specified daily limits for messages.

If the number of messages has not been exceeded then the message is sent out
via RWDE::PostMaster


sub _send_message {
  my ($self, $params) = @_;

  if (defined $$params{user_initiated}){
  my %loc_params = %{$params}; #copy the hash, to avoid side-effects


  $self->syslog_msg('devel', 'Sent ' .  $loc_params{template} . ' to ' . $loc_params{smtp_recipient});


=head2 check_limit()

Check to make sure that the mailing limit (default 5) has not been exceeded for today.


sub check_limit {
  my ($self, $params) = @_;
  if ($self->mail_count >= 5){
    throw RWDE::DataLimitException({ info => 'Max number of user inititiated emails reached for today.'});