| 1 | package Rapi::Blog::Util::Mailer; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | # ABSTRACT: General mailer object with defaults | ||
| 6 | |||
| 7 | use Moo; | ||
| 8 | use Types::Standard qw(:all); | ||
| 9 | |||
| 10 | use RapidApp::Util qw(:all); | ||
| 11 | |||
| 12 | require Module::Runtime; | ||
| 13 | use Email::Sender::Transport; | ||
| 14 | use Email::Sender::Transport::Sendmail; | ||
| 15 | use Email::Sender::Transport::SMTP; | ||
| 16 | use Email::Sender::Simple; | ||
| 17 | use Email::Simple; | ||
| 18 | use Email::Abstract; | ||
| 19 | use Path::Class qw/file dir/; | ||
| 20 | |||
| 21 | |||
| 22 | has 'smtp_config', is => 'ro', isa => Maybe[HashRef], default => sub { undef }; | ||
| 23 | |||
| 24 | sub send { | ||
| 25 | my $self = shift; | ||
| 26 | |||
| 27 | my %args = (scalar(@_) == 1) && (blessed($_[0]) || !ref($_[0])) | ||
| 28 | ? ( message => $_[0] ) | ||
| 29 | : (ref($_[0]) eq 'HASH') ? %{ $_[0] } : @_; # <-- arg as hash or hashref | ||
| 30 | |||
| 31 | |||
| 32 | # If we're called as a class method: | ||
| 33 | return $self->new(\%args)->send unless (blessed $self); | ||
| 34 | |||
| 35 | # We're an already created object, we shouldn't see any arguments: | ||
| 36 | die "->send() only accepts arguments when called as a class method" if (scalar(keys %args) > 0); | ||
| 37 | |||
| 38 | $self->_send | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | sub _send { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | $self->sent and die "Already sent."; | ||
| 46 | |||
| 47 | $self->envelope_to or die "Unable to send message - no recipient addresses were supplied or identified."; | ||
| 48 | |||
| 49 | Email::Sender::Simple->send($self->email, { | ||
| 50 | to => $self->envelope_to, | ||
| 51 | from => $self->envelope_from, | ||
| 52 | transport => $self->transport | ||
| 53 | }); | ||
| 54 | |||
| 55 | $self->sent(1); | ||
| 56 | } | ||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | sub BUILD { | ||
| 62 | my $self = shift; | ||
| 63 | |||
| 64 | # Perform initializations: | ||
| 65 | $self->init; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub init { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | # Perform initializations: | ||
| 72 | $self->transport; | ||
| 73 | $self->email; | ||
| 74 | |||
| 75 | $self->initialized or die "Unknown error; not initialized"; | ||
| 76 | |||
| 77 | $self | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | has 'message_file', is => 'ro', lazy => 1, default => sub { undef }, | ||
| 82 | isa => Maybe[InstanceOf['Path::Class::File']], coerce => sub { | ||
| 83 | return undef unless ($_[0]); | ||
| 84 | my $File = file($_[0]); | ||
| 85 | -f $File or die "message_file '$_[0]' not found or is not a regular file"; | ||
| 86 | $File | ||
| 87 | }, | ||
| 88 | |||
| 89 | |||
| 90 | |||
| 91 | has 'transport', | ||
| 92 | is => 'ro', | ||
| 93 | isa => ConsumerOf['Email::Sender::Transport'], | ||
| 94 | lazy => 1, | ||
| 95 | default => sub { | ||
| 96 | my $self = shift; | ||
| 97 | |||
| 98 | # If there is no custom smtp_config, we use the default Sendmail transport | ||
| 99 | # which relies of the sendmail binary to send mail from the localhost | ||
| 100 | return Email::Sender::Transport::Sendmail->new unless ($self->smtp_config); | ||
| 101 | |||
| 102 | my $cfg = clone($self->smtp_config); | ||
| 103 | |||
| 104 | # If there is a custom config, look for the special 'transport_class' option | ||
| 105 | # to override the default Email::Sender::Transport::SMTP. The rest of the | ||
| 106 | # params are passed directly to the transport constructor ->new() | ||
| 107 | my $class = $cfg->{transport_class} | ||
| 108 | ? delete $cfg->{transport_class} | ||
| 109 | : 'Email::Sender::Transport::SMTP'; | ||
| 110 | |||
| 111 | $class->new($cfg) | ||
| 112 | }; | ||
| 113 | |||
| 114 | |||
| 115 | has 'message', is => 'ro', lazy => 1, default => sub { | ||
| 116 | my $self = shift; | ||
| 117 | return scalar($self->message_file->slurp) if $self->message_file | ||
| 118 | }, isa => Maybe[Str]; | ||
| 119 | |||
| 120 | has 'body', is => 'ro', isa => Str, lazy => 1, default => sub { | ||
| 121 | my $self = shift; | ||
| 122 | $self->email->body || $self->default_body | ||
| 123 | }; | ||
| 124 | |||
| 125 | |||
| 126 | #has 'default_to', is => 'ro', isa => Str, default => sub { 'unspecified-address@unspecified-domain.com' }; | ||
| 127 | #has 'default_from', is => 'ro', isa => Str, default => sub { 'unspecified-address@unspecified-domain.com' }; | ||
| 128 | |||
| 129 | has 'default_to', is => 'ro', default => sub { undef }, isa => Maybe[ArrayRef[Str]], coerce => \&_array_coerce; | ||
| 130 | |||
| 131 | |||
| 132 | has 'default_from', is => 'ro', isa => Str, default => sub { '"Rapi::Blog administrator" <no-reply@rapid.app>' }; | ||
| 133 | has 'default_subject', is => 'ro', isa => Str, default => sub { '(no subject)' }; | ||
| 134 | has 'default_body', is => 'ro', isa => Str, default => sub { '' }; | ||
| 135 | |||
| 136 | has 'default_headers', is => 'ro', default => sub {[ | ||
| 137 | 'X-Mailer-Class' => __PACKAGE__ | ||
| 138 | ]}, isa => ArrayRef; | ||
| 139 | |||
| 140 | |||
| 141 | |||
| 142 | has 'envelope_from', is => 'ro', lazy => 1, default => sub { | ||
| 143 | my $self = shift; | ||
| 144 | $self->init; | ||
| 145 | ($self->_extract_addresses($self->from))[0] | ||
| 146 | }, isa => Str; | ||
| 147 | |||
| 148 | has 'envelope_to', is => 'ro', lazy => 1, default => sub { | ||
| 149 | my $self = shift; | ||
| 150 | $self->init->identified_recipients; | ||
| 151 | }, isa => Maybe[ArrayRef[Str]], coerce => \&_array_coerce; | ||
| 152 | |||
| 153 | |||
| 154 | has 'from', is => 'ro', lazy => 1, default => sub { | ||
| 155 | my $self = shift; | ||
| 156 | $self->init->exist_header->{from} || $self->default_from | ||
| 157 | }, isa => ArrayRef[Str], coerce => \&_array_coerce; | ||
| 158 | |||
| 159 | |||
| 160 | has 'to', is => 'ro', lazy => 1, default => sub { | ||
| 161 | my $self = shift; | ||
| 162 | $self->init->exist_header->{to} || $self->default_to | ||
| 163 | }, isa => ArrayRef[Str], coerce => \&_array_coerce; | ||
| 164 | |||
| 165 | |||
| 166 | has 'cc', is => 'ro', lazy => 1, default => sub { | ||
| 167 | my $self = shift; | ||
| 168 | $self->init->exist_header->{cc} || undef | ||
| 169 | }, isa => Maybe[ArrayRef[Str]], coerce => \&_array_coerce; | ||
| 170 | |||
| 171 | has 'bcc', is => 'ro', lazy => 1, default => sub { undef }, isa => Maybe[ArrayRef[Str]], coerce => \&_array_coerce; | ||
| 172 | |||
| 173 | has 'subject', is => 'ro', lazy => 1, default => sub { | ||
| 174 | my $self = shift; | ||
| 175 | $self->init; | ||
| 176 | $self->exist_header->{subject} || $self->defult_subject | ||
| 177 | }, isa => Str; | ||
| 178 | |||
| 179 | has 'importance', is => 'ro', isa => Maybe[Enum[qw/Low Normal High/]], | ||
| 180 | default => sub { undef }, coerce => sub { $_[0] ? ucfirst(lc($_[0])) : undef }; | ||
| 181 | |||
| 182 | |||
| 183 | |||
| 184 | sub _array_coerce { | ||
| 185 | my $val = shift or return undef; | ||
| 186 | $val = [$val] unless (ref($val)||'' eq 'ARRAY'); | ||
| 187 | scalar(@$val) > 0 ? $val : undef | ||
| 188 | } | ||
| 189 | |||
| 190 | has 'initialized', is => 'rw', isa => Bool, default => sub {0}; | ||
| 191 | has 'sent', is => 'rw', isa => Bool, default => sub {0}; | ||
| 192 | has 'exist_header', is => 'ro', isa => HashRef, default => sub {{}}; | ||
| 193 | has 'identified_recipients', is => 'ro', isa => ArrayRef[Str], default => sub {[]}; | ||
| 194 | |||
| 195 | has 'email', is => 'ro', init_arg => undef, lazy => 1, default => sub { | ||
| 196 | my $self = shift; | ||
| 197 | $self->initialized(0); | ||
| 198 | |||
| 199 | my @addr_collect = (); | ||
| 200 | |||
| 201 | my $email = Email::Abstract->new( $self->message | ||
| 202 | ? $self->message | ||
| 203 | : Email::Simple->create( header => $self->default_headers ) | ||
| 204 | ) or die "unknown error occured parsing message"; | ||
| 205 | |||
| 206 | my @recip_headers = qw/to cc bcc/; my %recip = map {$_=>1} @recip_headers; | ||
| 207 | my @headers = (@recip_headers,qw/from subject/); | ||
| 208 | for my $header (@headers) { | ||
| 209 | my $normal = ucfirst(lc($header)); | ||
| 210 | if($self->meta->get_attribute($header)->has_value($self)) { | ||
| 211 | my $value = $self->$header; | ||
| 212 | if(ref($value)||'' eq 'ARRAY') { # By rule, all attr headers which are ArrayRefs are addresses | ||
| 213 | $recip{$header} and push @addr_collect, @$value; | ||
| 214 | $value = join(', ',@$value); | ||
| 215 | } | ||
| 216 | $email->set_header( $normal => $value ) unless ($header eq 'bcc'); | ||
| 217 | } | ||
| 218 | else { | ||
| 219 | if(my $value = $email->get_header($normal)) { | ||
| 220 | $recip{$header} and push @addr_collect, $value; | ||
| 221 | $self->exist_header->{$header} = $value; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | if (my $importance = $self->importance) { | ||
| 227 | $email->set_header( Importance => $self->importance ); | ||
| 228 | $email->set_header( 'X-Priority' => | ||
| 229 | $self->importance eq 'High' ? 1 : | ||
| 230 | $self->importance eq 'Normal' ? 3 : | ||
| 231 | $self->importance eq 'Low' ? 5 : undef | ||
| 232 | ); | ||
| 233 | } | ||
| 234 | |||
| 235 | |||
| 236 | |||
| 237 | # Finally set additional default headers which haven't already been set: | ||
| 238 | my %headers = @{$self->default_headers}; | ||
| 239 | $email->get_header($_) or $email->set_header( $headers{$_} ) for (keys %headers); | ||
| 240 | |||
| 241 | # Do the same for body: user-supplied value first, then auto parsed value, then default as last resort | ||
| 242 | $self->meta->get_attribute('body')->has_value($self) and $email->set_body( $self->body ); | ||
| 243 | $email->get_body or $email->set_body( $self->body ); | ||
| 244 | |||
| 245 | push @addr_collect, $self->default_to if (scalar(@addr_collect) == 0 && $self->default_to); | ||
| 246 | |||
| 247 | @{$self->identified_recipients} = map { $self->_extract_addresses($_) } @addr_collect; | ||
| 248 | |||
| 249 | $self->initialized(1); | ||
| 250 | |||
| 251 | return $email | ||
| 252 | |||
| 253 | }, isa => InstanceOf['Email::Abstract']; | ||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | sub _extract_addresses { | ||
| 258 | my ($self, @vals) = @_; | ||
| 259 | |||
| 260 | my @addrs = (); | ||
| 261 | for my $val (@vals) { | ||
| 262 | if (ref($val)||'' eq 'ARRAY') { | ||
| 263 | push @addrs, $self->_extract_addresses(@$val); | ||
| 264 | } | ||
| 265 | else { | ||
| 266 | for my $EA (grep {$_} Email::Address->parse($val)) { | ||
| 267 | my $addr = $EA->address or next; | ||
| 268 | push @addrs, $EA->address; | ||
| 269 | } | ||
| 270 | } | ||
| 271 | } | ||
| 272 | uniq(@addrs) | ||
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | |||
| 277 | 1; | ||
| 278 | |||
| 279 | |||
| 280 | __END__ |