sharee.bike/copri4/main/src/Mod/MailTransport.pm
2022-01-12 20:09:55 +01:00

102 lines
2.6 KiB
Perl
Executable file

package Mod::MailTransport;
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (c) Rainer Gümpelein, TeilRad GmbH
#
# new emailing module
#
#perl -cw src/Mod/MailTransport.pm
#use lib "/var/www/copri4/shareeapp-operator/src";
#
use strict;
use warnings;
use POSIX;
use CGI ':standard';
use Email::MIME;
use IO::All;
use Net::SMTP;
use Try::Tiny;
use Config::General;
use Mod::Basework;
use Mod::DBtank;
use Data::Dumper;
sub new {
my $class = shift;
my $self = {};
bless($self,$class);
return $self;
}
my $q = new CGI;
my $bw = new Basework;
my $dbt = new DBtank;
my $mailx_file = "/var/www/copri4/shareeconf/mailx.cfg";
my $conf = Config::General->new($mailx_file);
my %mailxconf = $conf->getall;
sub send_mail(){
my $self = shift;
my $sendref = shift;
my $now_dt = strftime "%Y-%m-%d %H:%M:%S", localtime;
open(EMA, ">> $dbt->{copri_conf}->{logdir}/mailtransport.log");
my $mail_from = $sendref->{mail_from} || $mailxconf{$sendref->{mailxcfg}}->{mail_from};
my $mail_to = $sendref->{mail_to} || $mailxconf{$sendref->{mailxcfg}}->{mail_to};
my $filename = $sendref->{filename} || "";
print EMA "\n$now_dt, start mailing to: $mail_to | subject: $sendref->{subject}\n";
my $html = "<html><head><title>$sendref->{subject}</title></head><body style='text-align:left;border:0px solid silver;padding:15px;margin:2%;width:90%;'>\n";
$html .= "<div>$sendref->{message}</div>\n";
$html .= "<div>$sendref->{signature}</div>\n" if($sendref->{signature});
$html .= "</body></html>";
$bw->log("Trying send_mail:$0",$sendref,"");
if(ref($sendref) eq "HASH"){
my $smtp = Net::SMTP->new($mailxconf{$sendref->{mailxcfg}}->{mail_gateway},
Port => 465,
Hello => 'sharee.bike',
Timeout => 30,
Debug => 0,
SSL => 1,
);
$smtp->auth($mailxconf{$sendref->{mailxcfg}}->{sasl_username},$mailxconf{$sendref->{mailxcfg}}->{sasl_password});
$smtp->mail($mailxconf{$sendref->{mailxcfg}}->{mail_from});
if($dbt->{copri_conf}->{stage} eq "test"){
$mail_to = $mailxconf{$sendref->{mailxcfg}}->{mail_to};
$sendref->{subject} .= "* offline Test *";
}
if ($smtp->to($mail_to)) {
$smtp->data();
$smtp->datasend("To: $mail_to\n");
$smtp->datasend("Subject: $sendref->{subject}\nMIME-Version: 1.0\nContent-Type: text/html; charset=UTF-8 \n\n");
$smtp->datasend($html);
$smtp->dataend();
} else {
print EMA $smtp->message();
}
sleep 1;
#return $?;
}
print EMA "done mailing with state: $?\n";
print EMA "\n\n";
close EMA;
$bw->log("Success send_mail","","");
}#end mail_send
1;