Peace/lib/Peace/Email.pm

90 lines
2.6 KiB
Perl

package Peace::Email;
use v5.30.0;
use strict;
use warnings;
use Peace;
use Params::ValidationCompiler qw/validation_for/;
use Types::Standard qw/Str/;
use Email::MIME;
use Email::Sender::Simple;
use Email::Sender::Transport::SMTP;
sub new {
my $class = shift;
return bless {}, $class;
}
{
my $validator = validation_for(
params => {
text => { type => Str },
html => { type => Str },
to => { type => Str },
}
);
sub sendmail {
my $self = shift;
my %params = $validator->(@_);
my ( $text, $html, $to ) = @params{qw/text html to/};
my @parts = (
Email::MIME->create(
attributes => {
content_type => 'multipart/alternative',
},
parts => [
Email::MIME->create(
attributes => {
charset => 'UTF-8',
content_type => 'text/plain',
encoding => "quoted-printable",
disposition => 'inline',
},
body_str => $text,
),
Email::MIME->create(
attributes => {
charset => 'UTF-8',
content_type => 'text/html',
encoding => "quoted-printable",
disposition => 'inline',
},
body_str => $html,
)
]
)
);
my $email = Email::MIME->create(
header_str => [
From => Peace->new->peace_config->{smtp}{sasl_username},
To => $to,
],
attributes => {
encoding => 'base64',
content_type => 'multipart/mixed'
},
parts => [@parts],
);
Email::Sender::Simple::send( 'Email::Sender::Simple', $email,
{ transport => $self->generate_transport } );
}
}
sub generate_transport {
my $peace_config = Peace->new->peace_config;
my $transport = Email::Sender::Transport::SMTP->new(
hosts => [ $peace_config->{smtp}{smtp_host} ],
ssl => 1,
port => $peace_config->{smtp}{smtp_port},
sasl_username => $peace_config->{smtp}{sasl_username},
sasl_password => $peace_config->{smtp}{sasl_password},
);
return $transport;
}
1;