sharee.bike/copri4/main/src/Mod/MailTransport.pm

156 lines
4.4 KiB
Perl
Raw Normal View History

2022-01-14 10:49:45 +01:00
package MailTransport;
2021-12-30 12:05:56 +01:00
#
# 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 IO::All;
use Net::SMTP;
2022-01-31 14:55:05 +01:00
use MIME::Base64 qw( encode_base64 );
2021-12-30 12:05:56 +01:00
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;
2022-01-14 10:49:45 +01:00
sub mail_connect {
my $self = shift;
my $sendref = shift;
2021-12-30 12:05:56 +01:00
2022-01-14 10:49:45 +01:00
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,
2022-01-31 14:55:05 +01:00
Debug => 1,
2022-01-14 10:49:45 +01:00
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(){
2021-12-30 12:05:56 +01:00
my $self = shift;
2022-01-14 10:49:45 +01:00
my $smtp = shift;
my $mailxconf = shift;
2021-12-30 12:05:56 +01:00
my $sendref = shift;
2022-01-14 10:49:45 +01:00
#print Dumper($smtp);
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");
2022-01-14 10:49:45 +01:00
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};
2022-01-31 14:55:05 +01:00
my ($buf, $picture);
my $boundary = 'frontier';
my $pdfpath = "$dbt->{copri_conf}->{basedir}/$sendref->{syshost}/pdf";
my $attachBinaryFile = "sharee.pdf";
if(-f "$pdfpath/Rechnung-sharee_fr01-21084-4.pdf"){
$attachBinaryFile = "Rechnung-sharee_fr01-21084-4.pdf";
}
2022-01-14 10:49:45 +01:00
if($dbt->{copri_conf}->{stage} eq "test"){
$mail_to = $mailxconf->{$sendref->{mailxcfg}}->{mail_to};
$subject = "* offline Test * " . $sendref->{subject};
}
2021-12-30 12:05:56 +01:00
2022-01-14 10:49:45 +01:00
print EMA "\n$now_dt, start mailing to: $mail_to | subject: $subject\n";
2021-12-30 12:05:56 +01:00
2022-01-14 10:49:45 +01:00
my $html = "<html><head><title>$subject</title></head><body style='text-align:left;border:0px solid silver;padding:15px;margin:2%;width:90%;'>\n";
2022-01-12 20:09:55 +01:00
$html .= "<div>$sendref->{message}</div>\n";
$html .= "<div>$sendref->{signature}</div>\n" if($sendref->{signature});
$html .= "</body></html>";
2022-01-14 10:49:45 +01:00
$bw->log("Trying send_mail by $0",$mail_to,"");
2021-12-30 12:05:56 +01:00
if(ref($sendref) eq "HASH"){
2022-01-12 20:09:55 +01:00
if ($smtp->to($mail_to)) {
$smtp->data();
$smtp->datasend("To: $mail_to\n");
2022-01-31 14:55:05 +01:00
#$smtp->datasend("Subject: $subject\nMIME-Version: 1.0\nContent-Type: text/html; charset=UTF-8 \n\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("MIME-Version: 1.0\n");
#$smtp->datasend("Content-Type: text/html; charset=UTF-8 \n\n");
$smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$boundary\"\n");
$smtp->datasend("\n");
$smtp->datasend("--$boundary\n");
#$smtp->datasend("Content-type: text/plain\n");
#$smtp->datasend("\nToday\'s files are attached:\n\nHave a nice day! :)\n");
$smtp->datasend("Content-Type: text/html; charset=UTF-8 \n\n");
$smtp->datasend($html);
$smtp->datasend("\n");
$smtp->datasend("--$boundary\n");
#$smtp->datasend("Content-Type: image/jpeg; name=\"$attachBinaryFile\"\n");
$smtp->datasend("Content-Type: application/pdf; name=\"$attachBinaryFile\"\n");
$smtp->datasend("Content-Transfer-Encoding: base64\n");
$smtp->datasend("Content-Disposition: attachment; filename=\"$attachBinaryFile\"\n");
$smtp->datasend("\n");
if(1==1){
open(DAT, "$pdfpath/$attachBinaryFile") || die("Could not open $pdfpath/$attachBinaryFile");
binmode(DAT);
local $/=undef;
while (read(DAT, $picture, 4104)) {
$buf = &encode_base64( $picture );
$smtp->datasend($buf);
}
$smtp->datasend("\n");
close(DAT);
}
$smtp->datasend("--$boundary\r\n\r");
$smtp->dataend();#doesn't exit
$smtp->quit;
2022-01-12 20:09:55 +01:00
} 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;