| 1 | package RWDE::PostMaster; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base qw( RWDE::Singleton ); | ||
| 7 | |||
| 8 | use Template; | ||
| 9 | |||
| 10 | use Net::SMTP; | ||
| 11 | |||
| 12 | use RWDE::Configuration; | ||
| 13 | use RWDE::RObject; | ||
| 14 | |||
| 15 | my $unique_instance; | ||
| 16 | |||
| 17 | use vars qw($VERSION); | ||
| 18 | $VERSION = sprintf "%d", q$Revision: 527 $ =~ /(\d+)/; | ||
| 19 | |||
| 20 | =pod | ||
| 21 | |||
| 22 | =head1 RWDE::Postmaster | ||
| 23 | |||
| 24 | This class implements methods that detail the sending of email. Both verp and standard emailing are implemented here, as | ||
| 25 | well as methods for automatically reporting exceptions and hooks for assigning tickets to RT. | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | =head2 get_instance() | ||
| 30 | |||
| 31 | Return an instance of RWDE::Postmaster | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | sub get_instance { | ||
| 36 | my ($self, $params) = @_; | ||
| 37 | |||
| 38 | if (ref $unique_instance ne $self) { | ||
| 39 | $unique_instance = $self->new; | ||
| 40 | } | ||
| 41 | |||
| 42 | return $unique_instance; | ||
| 43 | } | ||
| 44 | |||
| 45 | =head2 initialize() | ||
| 46 | |||
| 47 | Initialize an instance of RWDE::Postmaster. This includes pulling some data from the config file in order to find an | ||
| 48 | SMTP server and preparing to handle a mail template. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub initialize { | ||
| 53 | my ($self, $params) = @_; | ||
| 54 | |||
| 55 | $self->{server} = RWDE::Configuration->get_SMTP(); | ||
| 56 | |||
| 57 | # create template object for future use | ||
| 58 | $self->{template} = Template->new( | ||
| 59 | { | ||
| 60 | TAG_STYLE => 'asp', | ||
| 61 | PROCESS => 'message.tt', | ||
| 62 | INCLUDE_PATH => RWDE::Configuration->get_root . '/templates/emailmessages', | ||
| 63 | VARIABLES => { | ||
| 64 | commify => \&RWDE::Utility::commify, | ||
| 65 | global => RWDE::Configuration->get_instance, | ||
| 66 | }, | ||
| 67 | } | ||
| 68 | ) or throw RWDE::DevelException({ info => 'Template::new failure.' }); | ||
| 69 | |||
| 70 | return (); | ||
| 71 | } | ||
| 72 | |||
| 73 | =head2 send_message ($smtp_sender, $smtp_recipient, $template) | ||
| 74 | |||
| 75 | Prepare or send a 1-to-1 message to the $smtp_recipient address, from $smtp_sender address, using $template as a template input | ||
| 76 | and the $params to populate the template. To alter the header from/to etc, edit the template. | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub send_message { | ||
| 81 | my ($self, $params) = @_; | ||
| 82 | |||
| 83 | my @required = qw( smtp_sender smtp_recipient template ); | ||
| 84 | RWDE::RObject->check_params({ required => \@required, supplied => $params }); | ||
| 85 | |||
| 86 | # Process the message thru the Template | ||
| 87 | my $output; | ||
| 88 | |||
| 89 | my $postmaster = RWDE::PostMaster->get_instance(); | ||
| 90 | |||
| 91 | my $template = $postmaster->{template}; | ||
| 92 | |||
| 93 | unless ($template->process($$params{template}, $params, \$output)) { | ||
| 94 | throw RWDE::DevelException({ info => $template->error() }); | ||
| 95 | } | ||
| 96 | |||
| 97 | my $mh = new Net::SMTP($postmaster->{server}) | ||
| 98 | or throw RWDE::DevelException({ info => 'PostMaster::CONNECT failure ' . $postmaster->{server} }); | ||
| 99 | |||
| 100 | $mh->mail($$params{smtp_sender}) | ||
| 101 | or throw RWDE::DevelException({ info => 'PostMaster::SMTP FROM failure: ' . $postmaster->{server} . '::' . $mh->message() }); | ||
| 102 | |||
| 103 | $mh->recipient($$params{smtp_recipient}) | ||
| 104 | or throw RWDE::DevelException({ info => 'PostMaster::SMTP TO failure: ' . $postmaster->{server} . '::' . $mh->message() . ' for: ' . $$params{smtp_recipient} }); | ||
| 105 | |||
| 106 | $mh->data() | ||
| 107 | or throw RWDE::DevelException({ info => 'PostMaster::DATA failure: ' . $postmaster->{server} . '::' . $mh->message() }); | ||
| 108 | $mh->datasend("Errors-To:$$params{smtp_sender}\n") | ||
| 109 | or throw RWDE::DevelException({ info => 'PostMaster::DATASEND failure: ' . $postmaster->{server} . '::' . $mh->message() }); | ||
| 110 | $mh->datasend($output) | ||
| 111 | or throw RWDE::DevelException({ info => 'PostMaster::DATASEND failure: ' . $postmaster->{server} . '::' . $mh->message() }); | ||
| 112 | |||
| 113 | $mh->dataend() | ||
| 114 | or throw RWDE::DevelException({ info => 'PostMaster::DATA END failure, message NOT sent: ' . $postmaster->{server} . '::' . $mh->message() }); | ||
| 115 | |||
| 116 | $mh->quit(); | ||
| 117 | |||
| 118 | return (); | ||
| 119 | } | ||
| 120 | |||
| 121 | =head2 send_verp_message($smtp_sender, $recipients, $template) | ||
| 122 | |||
| 123 | Prepare or send a 1-to-many message to all the addresses contained within $recipients, from $smtp_sender address, using $template as a template input | ||
| 124 | and the $params to populate the template. To alter the header from/to etc, edit the template. | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | sub send_verp_message { | ||
| 129 | my ($self, $params) = @_; | ||
| 130 | |||
| 131 | my @failed; | ||
| 132 | |||
| 133 | my @required = qw( smtp_sender recipients template); | ||
| 134 | RWDE::RObject->check_params({ required => \@required, supplied => $params }); | ||
| 135 | |||
| 136 | my $postmaster = RWDE::PostMaster->get_instance(); | ||
| 137 | |||
| 138 | my @recipients = @{ $$params{recipients} }; | ||
| 139 | if (!(@recipients > 0)) { | ||
| 140 | return; | ||
| 141 | } | ||
| 142 | |||
| 143 | # Process the message through the Template | ||
| 144 | my $output; | ||
| 145 | my $template = $postmaster->{template}; | ||
| 146 | |||
| 147 | unless ($template->process($$params{template}, $params, \$output)) { | ||
| 148 | throw RWDE::DevelException({ info => $template->error() }); | ||
| 149 | } | ||
| 150 | |||
| 151 | my $mh = new Net::SMTP($postmaster->{server}) | ||
| 152 | or throw RWDE::DevelException({ info => 'PostMaster::CONNECT failure' }); | ||
| 153 | |||
| 154 | #Limit to 1000 recipients per connection | ||
| 155 | # if we get more, just bucketize them | ||
| 156 | |||
| 157 | $mh->mail($$params{smtp_sender}, XVERP => 1) | ||
| 158 | or throw RWDE::DevelException({ info => 'PostMaster::SMTP XVERP failure: ' . $mh->message() }); | ||
| 159 | |||
| 160 | my @good_recipients = $mh->recipient(@recipients, { SkipBad => 1 }) or (); | ||
| 161 | |||
| 162 | $mh->data() | ||
| 163 | or throw RWDE::DevelException({ info => 'PostMaster::DATA failure: ' . $mh->message() }); | ||
| 164 | |||
| 165 | $mh->datasend($output) | ||
| 166 | or throw RWDE::DevelException({ info => 'PostMaster::DATASEND failure: ' . $mh->message() }); | ||
| 167 | |||
| 168 | $mh->dataend() | ||
| 169 | or throw RWDE::DevelException({ info => 'DATA END failure, message NOT sent: ' . $postmaster->{server} . '::' . $mh->message() }); | ||
| 170 | |||
| 171 | $mh->quit(); | ||
| 172 | |||
| 173 | return \@good_recipients; | ||
| 174 | } | ||
| 175 | |||
| 176 | =head2 send_support_message($topic, $question) | ||
| 177 | |||
| 178 | Prepare or send a message to the support queue requested within $topic, from a Sender (specified in config file) using support.tt as a template input. | ||
| 179 | The $params are used to to populate the template. To alter the header from/to etc, edit the template. | ||
| 180 | |||
| 181 | This is a somewhat niche method utilized with pre-established support systems such as RT. | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | sub send_support_message { | ||
| 186 | my ($self, $params) = @_; | ||
| 187 | |||
| 188 | my $topic = $$params{topic} | ||
| 189 | or throw RWDE::DataBadException({ info => 'No topic selected. Please select a topic so we can route your message properly' }); | ||
| 190 | |||
| 191 | my $question = $$params{question} | ||
| 192 | or throw RWDE::DataBadException({ info => 'Sorry, we didn\'t receive your message. Please try sending it again.' }); | ||
| 193 | |||
| 194 | $self->send_message( | ||
| 195 | { | ||
| 196 | smtp_sender => RWDE::Configuration->Sender, | ||
| 197 | smtp_recipient => RWDE::Configuration->$topic, | ||
| 198 | template => 'support.tt', | ||
| 199 | |||
| 200 | #-- template params | ||
| 201 | params => $params, | ||
| 202 | } | ||
| 203 | ); | ||
| 204 | |||
| 205 | return (); | ||
| 206 | } | ||
| 207 | |||
| 208 | =head2 send_report_message($topic, $question, $template) | ||
| 209 | |||
| 210 | Prepare or send a message to the ErrorReport address specified in the config file, from Sender - also specified in the config file. | ||
| 211 | Uses report.tt as a template input. The $params are used to to populate the template. To alter the header from/to etc, edit the template. | ||
| 212 | |||
| 213 | This method is extremely useful in addressing uncaught exceptions, if the system is configured correctly those messages will get sent | ||
| 214 | via this method. | ||
| 215 | |||
| 216 | =cut | ||
| 217 | |||
| 218 | sub send_report_message { | ||
| 219 | my ($self, $params) = @_; | ||
| 220 | |||
| 221 | $$params{smtp_sender} = RWDE::Configuration->Sender; | ||
| 222 | $$params{smtp_recipient} = RWDE::Configuration->ErrorReport; | ||
| 223 | $$params{template} = 'report.tt'; | ||
| 224 | |||
| 225 | $self->send_message($params); | ||
| 226 | |||
| 227 | return (); | ||
| 228 | } | ||
| 229 | |||
| 230 | 1; |