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

160 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,
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-02-01 20:53:23 +01:00
my $ret = 1;
2022-01-14 10:49:45 +01:00
my $now_dt = strftime "%Y-%m-%d %H:%M:%S", localtime;
open(EMA, ">> $dbt->{copri_conf}->{logdir}/mailTransport.log");
2022-01-12 20:09:55 +01:00
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";
2022-02-01 20:53:23 +01:00
my $attachBinaryFile = "";
2022-02-19 13:20:23 +01:00
my $filesize = 0;
2022-02-01 20:53:23 +01:00
if(-f "$pdfpath/$sendref->{attachment}"){
2022-02-19 13:20:23 +01:00
$filesize = -s "$pdfpath/$sendref->{attachment}";
2022-02-01 20:53:23 +01:00
$attachBinaryFile = "$sendref->{attachment}";
2022-01-31 14:55:05 +01:00
}
if($dbt->{copri_conf}->{stage} ne "live"){
2022-01-14 10:49:45 +01:00
$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";
2022-02-19 13:20:23 +01:00
print EMA "attachment: $pdfpath/$sendref->{attachment} | filesize: $filesize\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,"");
print EMA Dumper($sendref);
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\n");
$smtp->datasend("MIME-Version: 1.0\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");
2022-02-01 20:53:23 +01:00
if($attachBinaryFile){
$smtp->datasend("--$boundary\n");
#$smtp->datasend("Content-Type: image/jpeg; name=\"$attachBinaryFile\"\n");
$smtp->datasend("Content-Type: application/pdf; name=\"$attachBinaryFile\"\n");
2022-01-31 14:55:05 +01:00
2022-02-01 20:53:23 +01:00
$smtp->datasend("Content-Transfer-Encoding: base64\n");
$smtp->datasend("Content-Disposition: attachment; filename=\"$attachBinaryFile\"\n");
$smtp->datasend("\n");
2022-01-31 14:55:05 +01:00
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);
}
2022-02-01 20:53:23 +01:00
2022-01-31 14:55:05 +01:00
$smtp->datasend("--$boundary\r\n\r");
2022-02-01 20:53:23 +01:00
$smtp->dataend();
2022-01-31 14:55:05 +01:00
$smtp->quit;
2022-01-12 20:09:55 +01:00
} else {
print EMA $smtp->message();
}
sleep 1;
2022-02-01 20:53:23 +01:00
$ret = $?;
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","","");
2022-02-01 20:53:23 +01:00
return $ret;
2021-12-30 12:05:56 +01:00
}#end mail_send
1;