use
vars
qw($VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK)
;
$VERSION
=
'5.32'
;
@ISA
=
qw(Exporter)
;
@EXPORT
= (
qw( MailSet MailSend MailSent HTMLSend )
);
my
$mtypes
= MIME::Types->new;
my
%mailer
;
sub
MailSet {
my
%hash
=
@_
;
for
(
qw(mailsend logdir)
) {
$mailer
{
$_
} =
$hash
{
$_
}
if
(
$hash
{
$_
});
}
}
sub
MailSend {
my
%hash
=
@_
;
my
$errno
= 0;
$mailer
{mailsend} or
return
LogError(
"MailSend: mailsend not set"
);
$mailer
{logdir} or
return
LogError(
"MailSend: logdir not set"
);
my
$template
=
$hash
{template} or
return
LogError(
"MailSend: template not set"
);
my
$email
=
$hash
{recipient_email} or
return
LogError(
"MailSend: recipient_email not set"
);
my
$body
;
eval
{
$body
= Transform(
$template
,\
%hash
); };
return
LogError(
"MailSend: error=$@"
)
if
($@);
eval
{
$body
= decode_entities(
$body
) };
unless
(
$hash
{nowrap}) {
$Text::Wrap::columns
= 72;
$body
= wrap(
''
,
''
,
$body
);
}
if
(
$hash
{output}) {
my
$fh
= IO::File->new(
$hash
{output},
'a+'
) or
die
"Cannot write to file [$hash{output}]: $!"
;
$fh
->
binmode
(
':utf8'
);
print
$fh
$body
;
print
$fh
"\n\n#-----\n"
;
$fh
->
close
;
$mailer
{result} = 1;
$tvars
{mailer}{result} = 1;
}
else
{
my
$cmd
=
qq!| $mailer{mailsend} $email!
;
if
(
my
$fh
= IO::File->new(
$cmd
)) {
$fh
->
binmode
(
':utf8'
);
print
$fh
$body
;
$fh
->
close
;
$mailer
{result} = 1;
$tvars
{mailer}{result} = 1;
}
else
{
$mailer
{result} = 0;
$tvars
{mailer}{result} = 0;
$tvars
{mailer}{error} = $!;
}
unless
(
$mailer
{result}) {
my
@files
=
sort
glob
(
"$mailer{logdir}/mail*.eml"
);
my
$num
= 0;
(
$num
) = (
$files
[-1] =~ /mail(\d+).eml/)
if
(
@files
);
$num
++;
my
$file
=
sprintf
"%s/mail%06d.eml"
,
$mailer
{logdir},
$num
;
LogDebug(
"MailSend - $file"
);
my
$fh
= IO::File->new(
">$file"
) or
die
"Cannot write to file [$file]: $!"
;
binmode
(
$fh
,
':utf8'
);
print
$fh
$body
;
print
$fh
"\n\nCommand: $cmd\n"
;
print
$fh
"Error: $tvars{mailer}{error}\n"
;
$fh
->
close
;
$mailer
{file} =
$file
;
}
}
}
sub
MailSent {
return
$mailer
{result};
}
sub
HTMLSend {
my
%hash
=
@_
;
MIME::Lite->
send
(
'smtp'
,
$settings
{smtp},
Timeout
=>60);
my
$mail
= MIME::Lite->new(
From
=>
$hash
{from},
To
=>
$hash
{to},
Subject
=>
$hash
{subject},
Type
=>
'multipart/related'
);
unless
(
$mail
) {
LogError(
"HTMLSend: Error!"
);
return
;
}
if
(
$hash
{text}) {
my
$ref
= Transform(
$hash
{text},
$hash
{vars});
my
$text
=
$ref
;
$mail
->attach(
Type
=>
'text/text'
,
Data
=>
$text
)
if
(
$text
);
}
if
(
$hash
{html}) {
my
$ref
= Transform(
$hash
{html},
$hash
{vars});
my
$html
=
$ref
;
for
my
$path
(
$html
=~ m!href=
"([^"
]+)"!g) {
next
if
(
$path
=~ m!
$settings
{protregex}!);
my
$newpath
=
"$settings{docroot}/$settings{webpath}/$path"
;
$newpath
=~ s!//+!/!g;
$path
=~ s!href=
"$path"
!href=
"$newpath"
!g;
}
$mail
->attach(
Type
=>
'text/html'
,
Data
=>
$html
)
if
(
$html
);
}
for
(@{
$hash
{attach}}) {
if
(/\.pdf$/i) {
$mail
->attach(
Type
=>
'application/pdf '
,
Encoding
=>
'base64'
,
Path
=>
$_
,
Filename
=> basename(
$_
));
}
else
{
my
(
$type
,
$enc
) = _mtype(
$_
);
$mail
->attach(
Type
=>
$type
,
Encoding
=>
$enc
,
Path
=>
$_
,
Filename
=> basename(
$_
));
}
}
LogDebug(
"Mail="
.
$mail
->as_string());
eval
{
$mail
->
send
;};
if
($@) {
LogError(
"MailError: eval=[$@]"
) ;
$mailer
{result} = 0;
$tvars
{mailer}{result} = 0;
$tvars
{mailer}{error} = $@;
}
else
{
$mailer
{result} = 1;
$tvars
{mailer}{result} = 1;
}
}
sub
HTMLSendX {
my
%hash
=
@_
;
my
$path
=
$settings
{
'templates'
};
my
%config
= (
RELATIVE
=> 1,
ABSOLUTE
=> 1,
INCLUDE_PATH
=>
$path
,
INTERPOLATE
=> 0,
POST_CHOMP
=> 1,
TRIM
=> 1,
);
MIME::Lite->
send
(
'smtp'
,
$settings
{smtp},
Timeout
=>60);
my
$mail
= MIME::Lite::TT::HTML->new(
From
=>
$hash
{from},
To
=>
$hash
{to},
Subject
=>
$hash
{subject},
Encoding
=>
'quoted-printable'
,
Template
=> {
html
=>
$hash
{html},
text
=>
$hash
{text},
},
TmplOptions
=> \
%config
,
TmplParams
=> \
%tvars
,
);
unless
(
$mail
) {
LogError(
"HTMLSend: Error!"
);
return
;
}
for
(@{
$hash
{attach}}) {
if
(/\.pdf$/i) {
$mail
->attach(
Type
=>
'application/pdf '
,
Encoding
=>
'base64'
,
Path
=>
$_
,
Filename
=> basename(
$_
));
}
else
{
my
(
$type
,
$enc
) = _mtype(
$_
);
$mail
->attach(
Type
=>
$type
,
Encoding
=>
$enc
,
Path
=>
$_
,
Filename
=> basename(
$_
));
}
}
LogDebug(
"Mail="
.
$mail
->as_string());
eval
{
$mail
->
send
};
if
($@) {
LogError(
"MailError: eval=[$@]"
) ;
$mailer
{result} = 0;
$tvars
{mailer}{result} = 0;
$tvars
{mailer}{error} = $@;
}
else
{
$mailer
{result} = 1;
$tvars
{mailer}{result} = 1;
}
}
sub
_mtype {
my
$file
=
shift
;
my
$data
=
$mtypes
->by_suffix(
$file
);
return
@$data
;
}
1;