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 ;
2024-02-12 16:43:31 +01:00
#mailxcfg shareeconf/mailx.cfg
#hash data to send
sub sendrefhash {
my $ self = shift ;
my $ sendref = {
mailxcfg = > "mailx_default" ,
syshost = > "" ,
mail_from = > "" ,
mail_to = > "" ,
mail_bcc = > "" ,
c_id = > 0 ,
subject = > "" ,
message = > "" ,
signature = > "" ,
attachment = > "" ,
} ;
return $ sendref ;
}
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 ,
2023-01-27 06:55:30 +01:00
Debug = > 0 ,
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 ;
2023-08-09 20:45:44 +02:00
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 } ;
2023-07-20 07:14:28 +02:00
my $ mail_bcc = $ sendref - > { mail_bcc } || $ mailxconf - > { $ sendref - > { mailxcfg } } - > { mail_bcc } || "" ;
2022-01-14 10:49:45 +01:00
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
}
2022-02-15 13:17:10 +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 } ;
}
2023-07-20 07:14:28 +02: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>" ;
2023-08-01 14:26:20 +02:00
print EMA "Trying send_mail by $0 to $mail_to\n" ;
2023-01-23 12:56:14 +01:00
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 ) ) {
2023-07-20 07:14:28 +02:00
$ smtp - > mail ( "shareemail" ) ;
$ smtp - > to ( $ mail_to ) ;
$ smtp - > bcc ( $ mail_bcc ) if ( $ mail_bcc ) ;
2022-01-12 20:09:55 +01:00
$ smtp - > data ( ) ;
$ smtp - > datasend ( "To: $mail_to\n" ) ;
2023-07-20 07:14:28 +02:00
$ smtp - > datasend ( "Bcc: $mail_bcc\n" ) if ( $ mail_bcc ) ;
2022-01-31 14:55:05 +01:00
$ smtp - > datasend ( "Subject: $subject\n" ) ;
2023-08-09 20:45:44 +02:00
$ smtp - > datasend ( "\t\n" ) if ( ! $ attachBinaryFile ) ;
2023-07-05 06:23:29 +02:00
$ smtp - > datasend ( "MIME-Version: 1.0\n" ) ;
$ smtp - > datasend ( "Content-type: multipart/mixed;\n\tboundary=\"$boundary\"\n" ) ;
$ smtp - > datasend ( "\n" ) ;
2022-01-31 14:55:05 +01:00
2023-07-05 06:23:29 +02:00
$ smtp - > datasend ( "--$boundary\n" ) ;
$ smtp - > datasend ( "Content-Type: text/html; charset=UTF-8 \n\n" ) ;
$ smtp - > datasend ( "$html\n\n\n\n\n\n\n\n\n\n" ) ;
2022-01-31 14:55:05 +01:00
$ 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 ) ;
2023-07-05 06:23:29 +02:00
$ smtp - > datasend ( "--$boundary\r\n\r" ) ;
2022-01-31 14:55:05 +01:00
}
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
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 ;
2023-01-27 06:55:30 +01:00
$ bw - > log ( "done mailing with state:" , "$ret" , "" ) ;
2022-02-01 20:53:23 +01:00
return $ ret ;
2021-12-30 12:05:56 +01:00
} #end mail_send
2024-02-12 16:43:31 +01:00
#we use just db-selector hashes
sub mail_feedback2garage {
my $ self = shift ;
my $ crecord_content = shift ;
my $ contentpos = shift ;
my $ sendref = $ self - > sendrefhash ( ) ;
$ sendref - > { mail_to } = $ crecord_content - > { fleed_email } ;
2024-02-15 10:43:10 +01:00
$ sendref - > { subject } = "sharee.bike App Meldung" ;
$ sendref - > { message } = "Es ist folgende sharee.bike App Nachricht zu Mietrad $contentpos->{ct_name} an Station $contentpos->{int04} eingegangen:\n\n$contentpos->{txt01}\n\nFreundliche Grüße,\nIhr sharee.bike Mietradsystem" ;
2024-02-12 16:43:31 +01:00
$ sendref - > { message } =~ s/\n/\<br \/\>/g ; #TOD prepare_content
my ( $ smtp , $ mailxconf ) = $ self - > mail_connect ( $ sendref ) ;
my $ ret = $ self - > mail_transport ( $ smtp , $ mailxconf , $ sendref ) ;
return $ ret ;
}
2021-12-30 12:05:56 +01:00
1 ;