sharee.bike/copri4/main/src/scripts/mailTransportcms.pl

255 lines
6.7 KiB
Perl
Executable file

#!/usr/bin/perl
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (c) Rainer Gümpelein, TeilRad GmbH
#
#2023-01-02
#adapted from mailTransport.pl without bulkmail
#NEW
#managed text by operator cms
#
#Example
#./src/scripts/mailTransportcms.pl 'shareedms-operator' 'send_alarm2hotline' '[contentuser.c_id]' '[contenttheftpos.c_id]'
#
#
#
use vars qw($syshost);
BEGIN {
$syshost = $ARGV[0] || die 'syshost not defined';
}
use lib "/var/www/copri-bike/$syshost/src";
use strict;
use warnings;
use utf8;
use Encode;
use POSIX;
use Scalar::Util qw(looks_like_number);
use Lib::Config;
use Mod::DBtank;
use Mod::MailTransport;
use Data::Dumper;
my $cf = new Config;
my %varenv = $cf->envonline();
my $dbt = new DBtank;
my $mailtrans = new MailTransport;
my $now_dt = strftime "%Y-%m-%d %H:%M:%S", localtime;
#one contentadr.c_id OR email on command
my $todo = $ARGV[1] || "";
my $adr_id = $ARGV[2] || "";#takes ctt.c_id or ctadr.email
my $ct_id = $ARGV[3] || "";
my $attachment = $ARGV[4] || "";
open(EMA, ">> $dbt->{copri_conf}->{logdir}/mailTransportcms.log");
print EMA "\n$now_dt, start mailTransportcms.pl syshost: $syshost, todo:$todo, adr_id:$adr_id, ct_id:$ct_id, attachment:$attachment \n";
#mailxcfg is shareeconf/mailx.cfg <block> selection!
#hash data to send
my $sendref = {
mailxcfg => "mailx_default",
#mailxcfg => "mailx_admin",
syshost => "$syshost",
mail_to => "",
c_id => 0,
subject => "",
message => "",
signature => "",
attachment => "",
};
#my $project = $dbt->{operator}->{$varenv{dbname}}->{project} || die "project not defined";
my $oprefix = $dbt->{operator}->{$varenv{dbname}}->{oprefix} || "";
my $sendmail = {};
#send_cpdate_message, done by cronjob cpdate_check.pl
$sendmail = send_cpupdate_message($todo,$sendref,$adr_id) if(($todo eq "send_cpupdate_message" || $todo eq "send_proactive_cpupdate_message") && looks_like_number($adr_id));
#send_alarm2hotline, done by Ilockit_cloud.pl
$sendmail = send_alarm2hotline($sendref,$ct_id) if($todo eq "send_alarm2hotline" && looks_like_number($adr_id) && looks_like_number($ct_id));
if($sendmail->{c_id}){
sendmailjob($sendmail);
}else{
print EMA "Error, can not sendmailjob without c_id\n";
}
#send_cpupdate_message
sub send_cpupdate_message {
my $todo = shift;
my $sendref = shift;
my $adr_id = shift || "";
my $client_encoding = "iso-8859-1";
my $dbh_primary = $dbt->dbconnect_extern($dbt->{primary}->{sharee_primary}->{database}->{dbname},$client_encoding);
#TeilRad Kontakt
my $pref_ctu = {
table => "contentuser",
fetch => "one",
c_id => "1",#Kontakt-hotline
};
my $uadr = { c_id => 0 };
$uadr = $dbt->fetch_tablerecord($dbh_primary,$pref_ctu);
#CMS text
my $cms_message_key = "email-cpupdate-message";
$cms_message_key = "email-proactive-cpupdate-message" if($todo eq "send_proactive_cpupdate_message");
my $pref_cms = {
table => "contentuser",
fetch => "one",
ct_name => "$cms_message_key",
};
my $cms_prim = { c_id => 0, txt01 => '' };
$cms_prim = $dbt->fetch_tablerecord($dbh_primary,$pref_cms);
#Kunde
my $pref_adr = {
table => "contentadr",
fetch => "one",
c_id => "$adr_id",
};
my $cta = { c_id => 0 };
$cta = $dbt->fetch_tablerecord($dbh_primary,$pref_adr);
$sendref = prepare_content($sendref,$cta,$cms_prim,$uadr);
return $sendref;
}#end send_cpupdate_message
#prepare email by cms markup
sub prepare_content {
my $sendref = shift;
my $cta = shift;
my $cms_prim = shift;
my $uadr = shift;
my $globalconf_file = "/var/www/copri4/shareeconf/global.cfg";
my $conf = Config::General->new($globalconf_file);
my %globalconf = $conf->getall;
my $app_name = "App";
while (my ($key, $merchant) = each %{ $globalconf{merchant_ids} }) {
$app_name =~ s/$app_name/$merchant->{app_name}/ if($merchant->{app_name} && $cta->{int15} && $cta->{int15} == $merchant->{id});
}
my $subject = "TeilRad Mietradsystem";#default
$subject = $1 if($cms_prim->{txt01} =~ /--subject--(.*)--subject--/);
$cms_prim->{txt01} =~ s/--subject--$subject--subject--//;
$cms_prim->{txt01} =~ s/\n//;
$cms_prim->{txt01} =~ s/\n//;
my $signature = <<EOF
--
$uadr->{txt01}
$uadr->{txt03}
$uadr->{txt06}
$uadr->{txt07}
$uadr->{txt08}
$uadr->{txt84}
EOF
;
$sendref->{mail_to} = $cta->{txt08};
$sendref->{c_id} = $cta->{c_id};
$sendref->{subject} = $subject;
$sendref->{subject} =~ s/::app_name::/$app_name/;
$sendref->{subject} =~ s/Mein // if($sendref->{subject} =~ /^Mein/);
$sendref->{message} = $cms_prim->{txt01};
$sendref->{message} =~ s/::user_name::/$cta->{txt01}/;
$sendref->{message} =~ s/::app_name::/$app_name/g;
$sendref->{message} =~ s/::signature::/$signature/;
$sendref->{message} =~ s/\n/\<br \/\>/g;
return $sendref;
}#end prepare_content
#send_alarm2hotline
sub send_alarm2hotline {
my $sendref = shift;
my $ct_id = shift || "";
my $client_encoding = "iso-8859-1";
my $dbh = $dbt->dbconnect_extern($dbt->{operator}->{$varenv{dbname}}->{database}->{dbname},$client_encoding);
my $pref_ctu = {
table => "contentuser",
fetch => "one",
c_id => "1",#Kontakt-hotline
};
my $uadr = { c_id => 0 };
$uadr = $dbt->fetch_tablerecord($dbh,$pref_ctu);
$sendref->{mail_to} = $uadr->{txt08};
#without cms
my $cms_prim = { c_id => 0, txt01 => 'Folgendes Mietrad hat einen Diebstahlalarm ausgelöst' };
my $pref_ct = {
table => "contenttheftpos",
fetch => "one",
c_id => $ct_id,
};
my $ct = { c_id => 0 };
$ct = $dbt->fetch_tablerecord($dbh,$pref_ct);
$sendref->{c_id} = $ct->{c_id};
$sendref->{subject} = "Diebstahlalarm $oprefix$ct->{barcode}";
$sendref->{message} = <<EOF
$cms_prim->{txt01}
Bike: $oprefix$ct->{barcode}
Speed: $ct->{int07}
Meter: $ct->{int08}
GPS: $ct->{txt06}
Timestamp: $ct->{start_time}
Öffne das Alarmjournal für weitere Informationen:
<a href='$dbt->{operator}->{$varenv{dbname}}->{operatorDMS}/DMS/Alarmjournal'>$dbt->{operator}->{$varenv{dbname}}->{operatorDMS}/DMS/Alarmjournal</a>
Freundliche Grüße,
--
$uadr->{txt01}
$uadr->{txt03}
$uadr->{txt06}
$uadr->{txt84}
EOF
;
$sendref->{message} =~ s/\n/\<br \/\>/g;
return $sendref;
}
#sending mail job
sub sendmailjob {
my $sendref = shift || "";
#at first connect mailxchanger by mailx.cfg config
my ($smtp,$mailxconf) = $mailtrans->mail_connect($sendref);
#one mailing with mail address on command
if($sendref->{mail_to} =~ /\w\@\w/ && $sendref->{subject} && $sendref->{message}){
my $ret = "";
$ret = $mailtrans->mail_transport($smtp,$mailxconf,$sendref);
print EMA "done mailing with state to $sendref->{mail_to}: $ret\n";
}else{
print EMA "Error, no or false email address $sendref->{mail_to}, exit\n";
}
}
print EMA "\n\n";
close EMA;