lemonldap-ng/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Lib/SMTP.pm

191 lines
5.5 KiB
Perl
Raw Normal View History

##@file
# SMTP common functions
##@class
# SMTP common functions
2017-01-04 06:52:39 +01:00
package Lemonldap::NG::Portal::Lib::SMTP;
use strict;
2017-01-04 06:52:39 +01:00
use Mouse;
use String::Random;
use MIME::Lite;
2011-06-21 14:31:45 +02:00
use MIME::Base64;
2011-05-27 16:08:49 +02:00
use Encode;
our $VERSION = '2.0.0';
2017-01-04 06:52:39 +01:00
# PROPERTIES
2017-01-04 06:53:34 +01:00
has random => (
is => 'rw',
default => sub {
return String::Random->new;
}
);
has charset => (
is => 'rw',
default => sub { return $_[0]->{conf}->{mailCharset} || 'utf-8' }
);
2017-01-04 06:52:39 +01:00
# Generate a complex password based on a regular expression
# @param regexp regular expression
sub gen_password {
2017-01-04 06:53:34 +01:00
my ( $self, $regexp ) = @_;
2017-01-04 06:52:39 +01:00
return $self->random->randregex($regexp);
}
# Send mail
# @param mail recipient address
# @param subject mail subject
# @param body mail body
# @param html optional set content type to HTML
# @return boolean result
sub send_mail {
my ( $self, $mail, $subject, $body, $html ) = @_;
2017-01-14 20:31:48 +01:00
$self->lmLog( "send_mail called to send \"$subject\" to $mail", 'debug' );
2011-05-27 16:08:49 +02:00
# Encode the body with the given charset
2017-01-04 06:52:39 +01:00
$body = encode( $self->charset, $body );
$subject = encode( $self->charset, $subject );
2011-05-27 16:08:49 +02:00
# Debug messages
2017-01-14 20:31:48 +01:00
$self->lmLog( "SMTP From " . $self->conf->{mailFrom}, 'debug' );
$self->lmLog( "SMTP To " . $mail, 'debug' );
$self->lmLog( "SMTP Subject " . $subject, 'debug' );
$self->lmLog( "SMTP Body " . $body, 'debug' );
2010-03-01 21:32:28 +01:00
$self->lmLog( "SMTP HTML flag " . ( $html ? "on" : "off" ), 'debug' );
2017-01-14 20:31:48 +01:00
$self->lmLog( "SMTP Reply-To " . $self->conf->{mailReplyTo}, 'debug' )
if $self->conf->{mailReplyTo};
2011-05-27 11:41:13 +02:00
2011-05-27 16:30:14 +02:00
# Encode the subject
2016-12-29 07:25:07 +01:00
$subject = encode_base64( $subject, '' );
2017-01-04 06:52:39 +01:00
$subject =~ s/\s//gs;
2017-01-04 06:53:34 +01:00
$subject = '=?' . $self->charset . "?B?$subject?=";
2011-05-27 16:30:14 +02:00
# Detect included images (cid)
my %cid = ( $body =~ m/"cid:([^:]+):([^"]+)"/g );
# Replace cid in body
$body =~ s/"cid:([^:]+):([^"]+)"/"cid:$1"/g;
eval {
# Create message
my $message;
# HTML case
if ($html) {
$message = MIME::Lite->new(
2017-01-14 20:31:48 +01:00
From => $self->conf->{mailFrom},
To => $mail,
(
$self->conf->{mailReplyTo}
? ( "Reply-To" => $self->conf->{mailReplyTo} )
: ()
),
Subject => $subject,
Type => 'multipart/related',
);
# Attach HTML message
$message->attach(
2017-01-04 06:53:34 +01:00
Type => 'text/html; charset=' . $self->charset,
Data => qq{$body},
);
# Attach included images
foreach ( keys %cid ) {
$message->attach(
Type => "image/" . ( $cid{$_} =~ m/\.(\w+)/ )[0],
Id => $_,
2017-01-04 06:52:39 +01:00
Path => $self->p->{templateDir} . "/" . $cid{$_},
);
}
}
# Plain text case
else {
$message = MIME::Lite->new(
2017-01-14 20:31:48 +01:00
From => $self->conf->{mailFrom},
To => $mail,
2017-01-14 20:31:48 +01:00
"Reply-To" => $self->conf->{mailReplyTo},
Subject => $subject,
Type => 'TEXT',
Data => $body,
);
# Manage content type and charset
$message->attr( "content-type" => "text/plain" );
2017-01-04 06:52:39 +01:00
$message->attr( "content-type.charset" => $self->charset );
}
# Send the mail
2017-01-04 06:52:39 +01:00
$self->conf->{SMTPServer}
? $message->send(
2017-01-04 06:52:39 +01:00
"smtp", $self->conf->{SMTPServer},
AuthUser => $self->conf->{SMTPAuthUser},
AuthPass => $self->conf->{SMTPAuthPass}
)
: $message->send();
};
if ($@) {
$self->lmLog( "Send message failed: $@", 'error' );
return 0;
}
return 1;
}
## @method string getMailSession(string user)
# Check if a mail session exists
# @param user the value of the user key in session
# @return the first session id found or nothing if no session
sub getMailSession {
my ( $self, $user ) = @_;
2017-01-04 06:52:39 +01:00
my $moduleOptions = $self->conf->{globalStorageOptions} || {};
$moduleOptions->{backend} = $self->conf->{globalStorage};
my $module = "Lemonldap::NG::Common::Apache::Session";
# Search on mail sessions
my $sessions = $module->searchOn( $moduleOptions, "user", $user );
# Browse found sessions to check if it's a mail session
foreach my $id ( keys %$sessions ) {
2017-01-04 06:52:39 +01:00
my $mailSession = $self->p->getApacheSession( $id, 1 );
next unless ($mailSession);
return $id if ( $mailSession->data->{_type} =~ /^mail$/ );
}
# No mail session found, return empty string
return "";
}
2014-05-23 20:47:36 +02:00
## @method string getRegisterSession(string mail)
# Check if a register session exists
# @param mail the value of the mail key in session
# @return the first session id found or nothing if no session
sub getRegisterSession {
my ( $self, $mail ) = @_;
2014-05-23 20:47:36 +02:00
2017-01-04 06:52:39 +01:00
my $moduleOptions = $self->conf->{globalStorageOptions} || {};
$moduleOptions->{backend} = $self->conf->{globalStorage};
2014-05-23 20:47:36 +02:00
my $module = "Lemonldap::NG::Common::Apache::Session";
# Search on register sessions
my $sessions = $module->searchOn( $moduleOptions, "mail", $mail );
# Browse found sessions to check if it's a register session
foreach my $id ( keys %$sessions ) {
2017-01-04 06:52:39 +01:00
my $registerSession = $self->p->getApacheSession( $id, 1 );
next unless ($registerSession);
2014-05-23 20:47:36 +02:00
return $id if ( $registerSession->data->{_type} =~ /^register$/ );
}
# No register session found, return empty string
return "";
}
1;