| 1 | package Rapi::Blog::Model::Mailer; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | extends 'Catalyst::Model'; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | # ABSTRACT: Common interface for sending E-Mails | ||
| 10 | |||
| 11 | use RapidApp::Util qw(:all); | ||
| 12 | |||
| 13 | require Module::Runtime; | ||
| 14 | |||
| 15 | use Rapi::Blog::Util::Mailer; | ||
| 16 | |||
| 17 | use Email::MIME::CreateHTML; | ||
| 18 | |||
| 19 | before 'COMPONENT' => sub { | ||
| 20 | my $class = shift; | ||
| 21 | my $app_class = ref $_[0] || $_[0]; | ||
| 22 | |||
| 23 | my $cust_cnf = try{$app_class->config->{'Model::Mailer'}} || {}; | ||
| 24 | |||
| 25 | $class->config( | ||
| 26 | # Allow user-defined config overrides: | ||
| 27 | %$cust_cnf | ||
| 28 | ); | ||
| 29 | |||
| 30 | }; | ||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | sub send_mail { | ||
| 36 | my $self = shift; | ||
| 37 | my %opt = (ref($_[0]) eq 'HASH') ? %{ $_[0] } : @_; # <-- arg as hash or hashref | ||
| 38 | |||
| 39 | if(blessed($opt{to}) and $opt{to}->can('email')) { | ||
| 40 | my $User = $opt{to}; | ||
| 41 | |||
| 42 | my $email = $User->email or die "This user does not have an E-Mail address on file."; | ||
| 43 | $opt{to} = $User->full_name | ||
| 44 | ? join('','"',$User->full_name,'" <',$User->email,'>') | ||
| 45 | : $User->email; | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | %opt = (%{$self->config},%opt); | ||
| 50 | |||
| 51 | Rapi::Blog::Util::Mailer->send(\%opt) | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | __PACKAGE__->meta->make_immutable; | ||
| 58 | 1; | ||
| 59 | |||
| 60 | __END__ |