89 lines
2.2 KiB
Perl
89 lines
2.2 KiB
Perl
|
package Cualsea::Server::Mail;
|
||
|
|
||
|
use v5.30.0;
|
||
|
|
||
|
use strict;
|
||
|
use warnings;
|
||
|
|
||
|
use Const::Fast;
|
||
|
use Email::Sender::Transport::SMTP;
|
||
|
use Email::Sender::Simple qw/sendmail/;
|
||
|
use Email::MIME;
|
||
|
|
||
|
const my $MAIL_CONFIG_FILE => '/etc/cualsea/mail.conf';
|
||
|
const my $REPORT_TO_CONFIG_FILE => '/etc/cualsea/report_to';
|
||
|
const my $FROM_CONFIG_FILE => '/etc/cualsea/from';
|
||
|
|
||
|
sub new {
|
||
|
my $class = shift;
|
||
|
return bless {}, $class;
|
||
|
}
|
||
|
|
||
|
sub notify_service_down {
|
||
|
my $self = shift;
|
||
|
my $name = shift;
|
||
|
|
||
|
my $mime = Email::MIME->create(
|
||
|
header_str => [
|
||
|
From => $self->from,
|
||
|
To => [ $self->report_to ],
|
||
|
Subject => "Service $name down",
|
||
|
],
|
||
|
parts => [
|
||
|
Email::MIME->create(
|
||
|
attributes => {
|
||
|
content_type => 'text/plain',
|
||
|
disposition => 'attachment',
|
||
|
charset => 'UTF-8',
|
||
|
encoding => "8bit",
|
||
|
},
|
||
|
body_str => "$name is not up",
|
||
|
)
|
||
|
]
|
||
|
);
|
||
|
sendmail($mime, { transport => $self->transport });
|
||
|
}
|
||
|
|
||
|
sub report_to {
|
||
|
my $self = shift;
|
||
|
if (!defined $self->{report_to}) {
|
||
|
open my $fh, '<', $REPORT_TO_CONFIG_FILE or die "Unable to get mail to report from config file";
|
||
|
my $email = <$fh>;
|
||
|
chomp $email;
|
||
|
close $fh;
|
||
|
$self->{report_to} = $email;
|
||
|
}
|
||
|
return $self->{report_to};
|
||
|
}
|
||
|
|
||
|
sub from {
|
||
|
my $self = shift;
|
||
|
if (!defined $self->{from}) {
|
||
|
open my $fh, '<', $FROM_CONFIG_FILE or die "Unable to get mail From from config file";
|
||
|
my $email = <$fh>;
|
||
|
chomp $email;
|
||
|
close $fh;
|
||
|
$self->{from} = $email;
|
||
|
}
|
||
|
return $self->{from};
|
||
|
}
|
||
|
|
||
|
sub transport {
|
||
|
my $self = shift;
|
||
|
if (!defined $self->{transport}) {
|
||
|
my %parameters;
|
||
|
open my $fh, '<', $MAIL_CONFIG_FILE or die "Unable to get transport from config file";
|
||
|
while (my $line = <$fh>) {
|
||
|
my ($key, $value) = $line =~ /^(.*?)=(.*?)\s*$/;
|
||
|
if ($key eq 'hosts') {
|
||
|
$value = [ $value ];
|
||
|
}
|
||
|
$parameters{$key} = $value;
|
||
|
}
|
||
|
close $fh;
|
||
|
$self->{transport} = Email::Sender::Transport::SMTP->new(%parameters);
|
||
|
}
|
||
|
return $self->{transport};
|
||
|
}
|
||
|
1;
|