package 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; sub mail_connect { my $self = shift; my $sendref = shift; my $mailx_file = "/var/www/copri4/shareeconf/mailx.cfg"; my $conf = Config::General->new($mailx_file); my %mailxconf = $conf->getall; 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}); return ($smtp,\%mailxconf); } sub mail_transport(){ my $self = shift; my $smtp = shift; my $mailxconf = shift; my $sendref = shift; #print Dumper($smtp); 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 $subject = $sendref->{subject}; if($dbt->{copri_conf}->{stage} eq "test"){ $mail_to = $mailxconf->{$sendref->{mailxcfg}}->{mail_to}; $subject = "* offline Test * " . $sendref->{subject}; } print EMA "\n$now_dt, start mailing to: $mail_to | subject: $subject\n"; my $html = "$subject\n"; $html .= "
$sendref->{message}
\n"; $html .= "
$sendref->{signature}
\n" if($sendref->{signature}); $html .= ""; $bw->log("Trying send_mail by $0",$mail_to,""); if(ref($sendref) eq "HASH"){ if ($smtp->to($mail_to)) { $smtp->data(); $smtp->datasend("To: $mail_to\n"); $smtp->datasend("Subject: $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;