our
$VERSION
=
'2.0.12'
;
our
$transport
;
has
random
=> (
is
=>
'rw'
,
default
=>
sub
{
return
Lemonldap::NG::Common::Crypto::srandom();
}
);
has
charset
=> (
is
=>
'rw'
,
lazy
=> 1,
default
=>
sub
{
return
$_
[0]->{conf}->{mailCharset} ||
'utf-8'
}
);
has
transport
=> (
is
=>
'rw'
,
lazy
=> 1,
default
=>
sub
{
return
$transport
if
$transport
;
my
$conf
=
$_
[0]->{conf};
$transport
= Lemonldap::NG::Common::EmailTransport->new(
$conf
);
return
$transport
;
},
);
sub
loadMailTemplate {
my
(
$self
,
$req
,
$name
,
%prm
) =
@_
;
$prm
{cache} = 0
unless
defined
$prm
{cache};
$prm
{params}->{STATIC_PREFIX} =
$self
->p->staticPrefix;
$prm
{params}->{MAIN_LOGO} =
$self
->conf->{portalMainLogo};
my
%extra
=
$self
->p->can(
'tplParams'
)
?
$self
->p->tplParams(
$req
)
: ();
$prm
{params}->{
$_
} =
$extra
{
$_
}
for
keys
%extra
;
return
$self
->loadTemplate(
$req
,
$name
,
%prm
);
}
sub
translate {
my
(
$self
,
$req
) =
@_
;
my
$lang_code
=
$req
->cookies->{llnglanguage} ||
'en'
;
my
$json
=
$self
->conf->{templateDir} .
"/common/mail/$lang_code.json"
;
$json
=
$self
->conf->{templateDir} .
'/common/mail/en.json'
unless
( -f
$json
);
open
F,
'<'
,
$json
or
die
'Installation error: '
. $!
.
" ($self->{conf}->{templateDir}/$lang_code.json or $self->{conf}->{templateDir}/common/mail/en.json)"
;
$json
=
join
''
, <F>;
close
F;
my
$lang
= from_json(
$json
, {
allow_nonref
=> 1 } );
my
$langOver
=
from_json(
$self
->p->getTrOver(
$req
), {
allow_nonref
=> 1 } );
if
(
$langOver
) {
for
my
$k
(
keys
%{
$langOver
->{all} || {} } ) {
$lang
->{
$k
} =
$langOver
->{all}->{
$k
};
}
for
my
$k
(
keys
%{
$langOver
->{
$lang_code
} || {} } ) {
$lang
->{
$k
} =
$langOver
->{
$lang_code
}->{
$k
};
}
}
return
sub
{
(
$_
) =
@_
;
$$_
=~ s/\s+trspan=
"(\w+?)"
(.*?)>.*?</
"$2>"
.(
$lang
->{$1}||$1).
'<'
/gse;
$$_
=~ s/^(\w+)$/
$lang
->{$1}||$1/es;
};
}
sub
gen_password {
my
(
$self
,
$regexp
) =
@_
;
return
$self
->random->randregex(
$regexp
);
}
sub
send_mail {
my
(
$self
,
$mail
,
$subject
,
$body
,
$html
) =
@_
;
$self
->logger->info(
"send_mail called to send \"$subject\" to $mail"
);
$body
= encode(
$self
->charset, decode(
'utf-8'
,
$body
) );
$subject
= encode(
$self
->charset, decode(
'utf-8'
,
$subject
) );
$self
->logger->debug(
"SMTP From "
.
$self
->conf->{mailFrom} );
$self
->logger->debug(
"SMTP To "
.
$mail
);
$self
->logger->debug(
"SMTP Subject "
.
$subject
);
$self
->logger->debug(
"SMTP Body "
.
$body
);
$self
->logger->debug(
"SMTP HTML flag "
. (
$html
?
"on"
:
"off"
) );
$self
->logger->debug(
"SMTP Reply-To "
.
$self
->conf->{mailReplyTo} )
if
$self
->conf->{mailReplyTo};
$subject
= encode_base64(
$subject
,
''
);
$subject
=~ s/\s//gs;
$subject
=
'=?'
.
$self
->charset .
"?B?$subject?="
;
my
%cid
= (
$body
=~ m/
"cid:([^:]+):([^"
]+)"/g );
$body
=~ s/
"cid:([^:]+):([^"
]+)
"/"
cid:$1"/g;
eval
{
my
$message
;
if
(
$html
) {
$message
= MIME::Entity->build(
From
=>
$self
->conf->{mailFrom},
To
=>
$mail
,
(
$self
->conf->{mailReplyTo}
? (
"Reply-To"
=>
$self
->conf->{mailReplyTo} )
: ()
),
Subject
=>
$subject
,
Type
=>
'multipart/related'
,
Date
=> email_date,
);
$message
->attach(
Type
=>
'text/html; charset='
.
$self
->charset,
Data
=>
qq{$body}
,
);
foreach
(
keys
%cid
) {
$message
->attach(
Type
=>
"image/"
. (
$cid
{
$_
} =~ m/\.(\w+)/ )[0],
Id
=>
$_
,
Path
=>
$self
->conf->{templateDir} .
"/"
.
$self
->conf->{portalSkin} .
"/"
.
$cid
{
$_
},
);
}
}
else
{
$message
= MIME::Entity->build(
From
=>
$self
->conf->{mailFrom},
To
=>
$mail
,
"Reply-To"
=>
$self
->conf->{mailReplyTo},
Subject
=>
$subject
,
Type
=>
'TEXT'
,
Data
=>
$body
,
Type
=>
'text/plain'
,
Charset
=>
$self
->charset,
Date
=> email_date,
);
}
sendmail(
$message
->stringify,
(
$self
->transport ? {
transport
=>
$self
->transport } : () ) );
};
if
($@) {
$self
->logger->error(
"Send message failed: "
. ( $@->isa(
'Throwable::Error'
) ? $@->message : $@ ) );
return
0;
}
return
1;
}
sub
getMailSession {
my
(
$self
,
$user
) =
@_
;
my
$moduleOptions
=
$self
->conf->{globalStorageOptions} || {};
$moduleOptions
->{backend} =
$self
->conf->{globalStorage};
my
$module
=
"Lemonldap::NG::Common::Apache::Session"
;
my
$sessions
=
$module
->searchOn(
$moduleOptions
,
"user"
,
$user
);
foreach
my
$id
(
keys
%$sessions
) {
my
$mailSession
=
$self
->p->getApacheSession(
$id
, (
kind
=>
"TOKEN"
) );
next
unless
(
$mailSession
);
return
$mailSession
if
(
$mailSession
->data->{_type} =~ /^mail$/ );
}
return
""
;
}
sub
getRegisterSession {
my
(
$self
,
$mail
) =
@_
;
my
$moduleOptions
=
$self
->conf->{globalStorageOptions} || {};
$moduleOptions
->{backend} =
$self
->conf->{globalStorage};
my
$module
=
"Lemonldap::NG::Common::Apache::Session"
;
my
$sessions
=
$module
->searchOn(
$moduleOptions
,
"mail"
,
$mail
);
foreach
my
$id
(
keys
%$sessions
) {
my
$registerSession
=
$self
->p->getApacheSession(
$id
, (
kind
=>
"TOKEN"
) );
next
unless
(
$registerSession
);
return
$id
if
(
$registerSession
->data->{_type}
and
$registerSession
->data->{_type} =~ /^register$/ );
}
return
""
;
}
1;