| 1 | package RWDE::Mailing; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use vars qw($VERSION); | ||
| 7 | $VERSION = sprintf "%d", q$Revision: 507 $ =~ /(\d+)/; | ||
| 8 | |||
| 9 | =pod | ||
| 10 | |||
| 11 | =head1 RWDE::Mailing | ||
| 12 | |||
| 13 | Mailing interface for objects that can receive emails. | ||
| 14 | |||
| 15 | Throttling is also provided to keeping daily count of emails sent under control. | ||
| 16 | |||
| 17 | To be clear this is meant to be used as a base class and derived classes are required to | ||
| 18 | implement certain methods so the facility works correctly. | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | =head2 get_email() | ||
| 23 | |||
| 24 | Retrieve the class defined email address. This is specifically assigned within the RWDE | ||
| 25 | Object hash for the purpose of dispatching emails. | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | sub get_email { | ||
| 30 | my ($self, $params) = @_; | ||
| 31 | |||
| 32 | my $email_field = $self->{_email}; | ||
| 33 | |||
| 34 | throw RWDE::DevelException({ info => " $self does not have an email address" }) | ||
| 35 | unless defined $email_field; | ||
| 36 | |||
| 37 | return $self->$email_field; | ||
| 38 | } | ||
| 39 | |||
| 40 | =head2 send_message() | ||
| 41 | |||
| 42 | This method is required to be implemented by your derived class. If you don't implement it | ||
| 43 | an DevelException will be thrown upon invocation. | ||
| 44 | |||
| 45 | The implementation for this method should include a call to _send_message in order for the interface | ||
| 46 | to work end to end. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub send_message { | ||
| 51 | my ($self, $params) = @_; | ||
| 52 | |||
| 53 | my $class = ref $self || $self; | ||
| 54 | |||
| 55 | throw RWDE::DevelException({ info =>"Class $class does not implement send_message" }); | ||
| 56 | |||
| 57 | return(); | ||
| 58 | } | ||
| 59 | |||
| 60 | =head2 _send_message() | ||
| 61 | |||
| 62 | Verify that that record has no exceeded specified daily limits for messages. | ||
| 63 | |||
| 64 | If the number of messages has not been exceeded then the message is sent out | ||
| 65 | via RWDE::PostMaster | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub _send_message { | ||
| 70 | my ($self, $params) = @_; | ||
| 71 | |||
| 72 | if (defined $$params{user_initiated}){ | ||
| 73 | $self->check_limit(); | ||
| 74 | $self->mail_count($self->mail_count+1); | ||
| 75 | $self->update_record(); | ||
| 76 | } | ||
| 77 | |||
| 78 | my %loc_params = %{$params}; #copy the hash, to avoid side-effects | ||
| 79 | |||
| 80 | RWDE::PostMaster->send_message($params); | ||
| 81 | |||
| 82 | $self->syslog_msg('devel', 'Sent ' . $loc_params{template} . ' to ' . $loc_params{smtp_recipient}); | ||
| 83 | |||
| 84 | return(); | ||
| 85 | } | ||
| 86 | |||
| 87 | =head2 check_limit() | ||
| 88 | |||
| 89 | Check to make sure that the mailing limit (default 5) has not been exceeded for today. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub check_limit { | ||
| 94 | my ($self, $params) = @_; | ||
| 95 | |||
| 96 | if ($self->mail_count >= 5){ | ||
| 97 | throw RWDE::DataLimitException({ info => 'Max number of user inititiated emails reached for today.'}); | ||
| 98 | } | ||
| 99 | |||
| 100 | return(); | ||
| 101 | } | ||
| 102 | |||
| 103 | 1; | ||
| 104 |