2021-12-30 12:05:56 +01:00
|
|
|
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;
|
|
|
|
|
2022-01-12 20:09:55 +01:00
|
|
|
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};
|
2021-12-30 12:05:56 +01:00
|
|
|
my $filename = $sendref->{filename} || "";
|
|
|
|
|
2022-01-12 20:09:55 +01:00
|
|
|
print EMA "\n$now_dt, start mailing to: $mail_to | subject: $sendref->{subject}\n";
|
2021-12-30 12:05:56 +01:00
|
|
|
|
2022-01-12 20:09:55 +01:00
|
|
|
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,"");
|
2021-12-30 12:05:56 +01:00
|
|
|
|
|
|
|
if(ref($sendref) eq "HASH"){
|
|
|
|
|
2022-01-12 20:09:55 +01:00
|
|
|
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 $?;
|
2021-12-30 12:05:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-12 20:09:55 +01:00
|
|
|
print EMA "done mailing with state: $?\n";
|
|
|
|
print EMA "\n\n";
|
|
|
|
close EMA;
|
|
|
|
|
|
|
|
$bw->log("Success send_mail","","");
|
2021-12-30 12:05:56 +01:00
|
|
|
}#end mail_send
|
|
|
|
|
|
|
|
1;
|