From 9082b161dea206fe41d31999b90c3125524079dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rainer=20G=C3=BCmpelein?= Date: Wed, 12 Jan 2022 20:09:55 +0100 Subject: [PATCH] MailTransport refactoring --- copri4/main/src/Mod/APIjsonserver.pm | 2 +- copri4/main/src/Mod/Indexsharee.pm | 4 +- copri4/main/src/Mod/MailTransport.pm | 142 ++++++--------------- copri4/main/src/Mod/newsletter_tink.pl | 2 +- copri4/main/src/scripts/newsletter_post.pl | 133 ------------------- 5 files changed, 44 insertions(+), 239 deletions(-) delete mode 100755 copri4/main/src/scripts/newsletter_post.pl diff --git a/copri4/main/src/Mod/APIjsonserver.pm b/copri4/main/src/Mod/APIjsonserver.pm index 49beadd..1e6cd2c 100755 --- a/copri4/main/src/Mod/APIjsonserver.pm +++ b/copri4/main/src/Mod/APIjsonserver.pm @@ -283,7 +283,7 @@ elsif($q->param('request') eq "booking_cancel" || $q->param('request') eq "booki #keep in mind, it works on operator dependency $bw->log("user_miniquery via $varenv{dbname} exist count:",$authraw->{int23},""); #Nur für project=Bayern und für Entwickler aktiviert - if($booking_values->{state} eq "available" && (($dbt->{operator}->{$varenv{dbname}}->{project} eq "Bayern" && $authraw->{int23} >= 1 && $authraw->{int23} < 4) || ($authraw->{c_id} == 1842 || $authraw->{c_id} == 5781 || $authraw->{c_id} == 11765 || $authraw->{c_id} == 1843))){ + if($booking_values->{state} eq "available" && $dbt->{operator}->{$varenv{dbname}}->{project} eq "Bayern" && (($authraw->{int23} >= 1 && $authraw->{int23} < 4) || ($authraw->{c_id} == 1842 || $authraw->{c_id} == 5781 || $authraw->{c_id} == 11765 || $authraw->{c_id} == 1843))){ #TODO $ user_miniquery have to be in db table on primary $bw->log("user_miniquery communicated to user ID",$authraw->{c_id},""); diff --git a/copri4/main/src/Mod/Indexsharee.pm b/copri4/main/src/Mod/Indexsharee.pm index fa10ffe..0af7515 100755 --- a/copri4/main/src/Mod/Indexsharee.pm +++ b/copri4/main/src/Mod/Indexsharee.pm @@ -203,7 +203,7 @@ sub handler { #we need this to get $R::sessionid to FormEdit #if(length($coo) > 20 && !$q->cookie(-name=>'domcookie')){ ($api_return,$users_sharee) = $apif->auth_verify($q,$author->{authcookie},""); - if($R::failure =~ /\w+/ || ($users_sharee->{txt31} && $users_sharee->{txt31} =~ /\w/)){ + if(($returnwww && $returnwww =~ /\w+/) || ($users_sharee->{txt31} && $users_sharee->{txt31} =~ /\w/)){ print redirect("$varenv{wwwhost}/$varenv{mandant}/Account/$varenv{accounting_1}?sessionid=$coo"); exit 0; }else{ @@ -456,7 +456,7 @@ sub handler { $dbt->update_one($dbh,$update_adr,"int12=$vde_on_fail");#Vde } - $tk->emailack($users_sharee->{c_id}) if($users_sharee->{int04} != 1); + #$tk->emailack($users_sharee->{c_id}) if($users_sharee->{int04} != 1); $dbt->update_operatorsloop($varenv{dbname},$users_sharee->{c_id},"update"); print redirect("$varenv{wwwhost}/$varenv{mandant}/$varenv{profile}?$returnwww$session_and"); diff --git a/copri4/main/src/Mod/MailTransport.pm b/copri4/main/src/Mod/MailTransport.pm index 5dd7fc0..6dbc042 100755 --- a/copri4/main/src/Mod/MailTransport.pm +++ b/copri4/main/src/Mod/MailTransport.pm @@ -14,14 +14,10 @@ use POSIX; use CGI ':standard'; use Email::MIME; use IO::All; -use Email::Sender::Simple qw(sendmail); use Net::SMTP; use Try::Tiny; use Config::General; -use Sys::Hostname; -my $hostname = hostname; -use Lib::Config; use Mod::Basework; use Mod::DBtank; use Data::Dumper; @@ -34,7 +30,6 @@ sub new { } my $q = new CGI; -my $cf = new Config; my $bw = new Basework; my $dbt = new DBtank; @@ -47,117 +42,60 @@ sub send_mail(){ my $sendref = shift; my $now_dt = strftime "%Y-%m-%d %H:%M:%S", localtime; - my $mail_from = $sendref->{mail_from} || $mailxconf{mailx_default}->{mail_from}; - my $mail_to = $sendref->{mail_to} || $mailxconf{mailx_default}->{mail_to}; - my $subject = $sendref->{subject} || "subject fails"; + 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} || ""; - my $message = $sendref->{message} || "Failure, no message\n"; - #If caller use utf8, message is encoded as utf-8 - #$message = Encode::encode('utf-8', Encode::decode('iso-8859-1', $message)); - #Special tink statisk, readdir with filename - #$filename like "2018-12" - my @files = ($filename); + print EMA "\n$now_dt, start mailing to: $mail_to | subject: $sendref->{subject}\n"; - #disabled because of zipping - #TODO, check what happens about Statistik-occubike_station_2019-1-konrad.csv - #Okay, send_mail breaks on verylong Statistik lines. CSV zipping is the solution - #@files = $lb->read_dirfiles("$dbt->{copri_conf}->{basedir}/csv",$filename,"file","") if($filename =~ /\d+-\d+/); + my $html = "$sendref->{subject}\n"; + $html .= "
$sendref->{message}
\n"; + $html .= "
$sendref->{signature}
\n" if($sendref->{signature}); + $html .= ""; - if($dbt->{copri_conf}->{stage} eq "test"){ - $mail_to = $mailxconf{mailx_default}->{mail_to}; - $subject .= " * offline Test *"; - } $bw->log("Trying send_mail:$0",$sendref,""); - if(ref($sendref) eq "HASH"){ - $mailxconf{mailx_default}->{sasl_password} = Encode::encode('iso-8859-1', Encode::decode('utf-8', $mailxconf{mailx_default}->{sasl_password})); - my $transport = Email::Sender::Transport::SMTPS->new( - host => "$mailxconf{mailx_default}->{mail_gateway}", - ssl => 'ssl', - port => 465, - sasl_username => "$mailxconf{mailx_default}->{sasl_username}", - sasl_password => "$mailxconf{mailx_default}->{sasl_password}", - helo => "$mailxconf{mailx_default}->{mail_from}", - debug => 1, - ); + 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 *"; + } - #multipart message - #Email::MIME !!! - if(1==1){ - my @parts = (); + 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(); + } - my $parts_1 = ( - Email::MIME->create( - attributes => { - content_type => "text/plain", - #disposition => "attachment", - encoding => "quoted-printable", - charset => "UTF-8", - }, - body_str => "$sendref->{message}", - ), - ); - push(@parts,$parts_1); - - - foreach $filename (@files){ - if($filename){ - my $parts_0 = ( - Email::MIME->create( - attributes => { - filename => "$filename", - #content_type => "application/pdf", - #content_type => "text/csv", - content_type => "application/octet-stream", - disposition => "attachment", - encoding => "quoted-printable", - name => "$filename", - }, - body => io( "$dbt->{copri_conf}->{basedir}/csv/$filename" )->binary->all, - ), - ); - - $bw->log("Attachment:$dbt->{copri_conf}->{basedir}/csv/$filename","",""); - push(@parts,$parts_0); - } - } - - $message = Email::MIME->create( - header_str => [ From => "$mail_from", - To => "$mail_to", - Subject => "$subject", ], - parts => [ @parts ], - ); - #$message->charset_set( 'UTF-8' ); - print $message->as_string; - }#if not Email::Mime - else{ - - $message = Email::Simple->create( - header => [ - From => "$mail_from", - To => "$mail_to", - Subject => "$subject", - ], - body => "$sendref->{message}", - ); - } - - try { - sendmail($message, { transport => $transport }); - } catch { - $bw->log("FAILURE send_mail:$0",$sendref,""); - warn "Error sending mail: $_"; - }; + 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; diff --git a/copri4/main/src/Mod/newsletter_tink.pl b/copri4/main/src/Mod/newsletter_tink.pl index 2e6366b..81d6c71 100755 --- a/copri4/main/src/Mod/newsletter_tink.pl +++ b/copri4/main/src/Mod/newsletter_tink.pl @@ -139,7 +139,7 @@ my $ctadr = &get_contentadr($c_id,$emailpw) if($c_id =~ /^\d+$/ || $emailpw =~ / #$varenv{sasl_password} = Encode::encode('iso-8859-1', Encode::decode('utf-8', $varenv{sasl_password})); - $smtp->auth($mailxconf{mailx_default}->{sasl_username},$mailxconf{mailx}->{sasl_password}); + $smtp->auth($mailxconf{mailx_default}->{sasl_username},$mailxconf{mailx_default}->{sasl_password}); $smtp->mail($mailxconf{mailx_default}->{mail_from}); my $smtp_return = ""; diff --git a/copri4/main/src/scripts/newsletter_post.pl b/copri4/main/src/scripts/newsletter_post.pl deleted file mode 100755 index ac343e4..0000000 --- a/copri4/main/src/scripts/newsletter_post.pl +++ /dev/null @@ -1,133 +0,0 @@ -#!/usr/bin/perl -w - -#2021-03-17 -#redisgn because of BEGIN, use Net::SMTP; and executed in src/scripts -# -#sudo su www-data -c "./src/scripts/newsletter_post.pl 'shareedms-primary' 'send_cardexpire'" -# -#TODO, migrate it all to MailTransport.pm -# -# -use vars qw($syshost); - -BEGIN { - $syshost = $ARGV[0] || exit 1; -} -use lib "/var/www/copri-bike/$syshost/src"; - - -use strict; -use warnings; -use utf8; -use Encode; -use CGI ':standard'; -use DBI; -use POSIX; -use Email::MIME; -use IO::All; -use Email::MIME::CreateHTML; -use Email::Sender::Simple qw(sendmail); -use Net::SMTP; -use Try::Tiny; -use URI::Encode; -my $uri_encode = URI::Encode->new( { encode_reserved => 1 } ); -use Data::Dumper; - -use Sys::Hostname; -my $hostname = hostname; - -use Lib::Config; -my $q = new CGI; -my $cf = new Config; -my %varenv = $cf->envonline("","$syshost"); - -my $today = strftime("%d.%m.%Y %H:%M:%S",localtime(time)); -my $todo = $ARGV[1]; - -open(EMA, ">> $varenv{logdir}/newsletter_post.log"); -print EMA "\n$today, start mailing\n"; -print EMA "'$todo\n"; - -my $smtp_return = ""; -if($todo eq "send_cardexpire"){ - my ($mail_to,$subject,$body,$signature) = send_cardexpire(\%varenv); - $smtp_return = transport(\%varenv,$mail_to,$subject,$body,$signature); -} - -#---------------------------------------------------- -sub send_cardexpire { - my $varenv = shift; - - my $mail_to = "ragu\@gnu-systems.de"; - my $name = "test"; - - my $subject = "Fahrradmietsystem Konstanz"; - my $body = <https://www.stadtwerke-konstanz.de/mobilitaet/rad-mietsystem ) haben Sie die Möglichkeit Ihre Daten zu überprüfen und ggf. zu erneuern. -Kontaktieren Sie uns bitte falls Ihr Account für den Verleih nicht automatisch freigeschaltet wurde. - -Für weitere Fragen wenden Sie sich bitte an unsere Buchhaltung unter: buchhaltung\@fahrradspezialitaeten.com oder telefonisch 0761/5158912 (Mo, Mi, Fr 9-12 Uhr) - -EOF -; - $body =~ s/\n/\
/g; - - my $signature = ""; - - return ($mail_to,$subject,$body,$signature); -} -#--------------------------------------------------- -# -sub transport { - my $varenv = shift; - my $mail_to = shift; - my $subject = shift; - my $body = shift; - my $signature = shift; - - my $html = "$subject\n"; - $html .= "
$body
\n"; - $html .= "
$signature
\n"; - $html .= ""; - - my $smtp = Net::SMTP->new($varenv->{mail_gateway}, - Port => 465, - Hello => 'TeilRad', - Timeout => 30, - Debug => 0, - SSL => 1, - ); - $smtp->auth($varenv->{sasl_username},$varenv->{sasl_password}); - $smtp->mail($varenv->{mail_from}); - - if($hostname ne "ginger"){ - $mail_to = $varenv->{mail_testto}; - $subject .= "* offline Test *"; - } - - 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(); - print EMA "$?\n"; - } else { - print EMA $smtp->message(); - } - - sleep 1; - return $?; -} -#---------------------------------------------------- - -print EMA "done mailing: $?\n"; -print EMA "\n\n"; -close EMA; - -1;