2009-12-21 23:28:38 +01:00
|
|
|
##@file
|
|
|
|
# SMTP common functions
|
|
|
|
|
|
|
|
##@class
|
|
|
|
# SMTP common functions
|
|
|
|
package Lemonldap::NG::Portal::_SMTP;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
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;
|
2009-12-21 23:28:38 +01:00
|
|
|
|
2014-10-27 12:19:25 +01:00
|
|
|
our $VERSION = '1.4.2';
|
2009-12-21 23:28:38 +01:00
|
|
|
|
2010-10-03 12:26:50 +02:00
|
|
|
## @method string gen_password(string regexp)
|
2009-12-21 23:28:38 +01:00
|
|
|
# Generate a complex password based on a regular expression
|
|
|
|
# @param regexp regular expression
|
|
|
|
# @return complex password
|
|
|
|
sub gen_password {
|
2010-03-01 21:32:28 +01:00
|
|
|
my $self = shift;
|
2009-12-21 23:28:38 +01:00
|
|
|
my $regexp = shift;
|
|
|
|
|
|
|
|
my $random = new String::Random;
|
2010-01-21 18:38:55 +01:00
|
|
|
return $random->randregex($regexp);
|
2009-12-21 23:28:38 +01:00
|
|
|
}
|
|
|
|
|
2010-10-03 12:26:50 +02:00
|
|
|
## @method int send_mail(string mail, string subject, string body, string html)
|
2010-01-21 18:38:55 +01:00
|
|
|
# Send mail
|
2010-01-22 12:25:37 +01:00
|
|
|
# @param mail recipient address
|
|
|
|
# @param subject mail subject
|
|
|
|
# @param body mail body
|
|
|
|
# @param html optional set content type to HTML
|
2009-12-21 23:28:38 +01:00
|
|
|
# @return boolean result
|
2010-01-21 18:38:55 +01:00
|
|
|
sub send_mail {
|
2016-01-02 10:29:05 +01:00
|
|
|
my ( $self, $mail, $subject, $body, $html ) = @_;
|
2009-12-21 23:28:38 +01:00
|
|
|
|
2011-05-27 16:08:49 +02:00
|
|
|
# Set charset
|
|
|
|
my $charset = $self->{mailCharset} ? $self->{mailCharset} : "utf-8";
|
|
|
|
|
|
|
|
# Encode the body with the given charset
|
2013-01-25 12:05:34 +01:00
|
|
|
$body = encode( $charset, $body );
|
|
|
|
$subject = encode( $charset, $subject );
|
2011-05-27 16:08:49 +02:00
|
|
|
|
|
|
|
# Debug messages
|
2010-03-01 21:32:28 +01:00
|
|
|
$self->lmLog( "SMTP From " . $self->{mailFrom}, 'debug' );
|
|
|
|
$self->lmLog( "SMTP To " . $mail, 'debug' );
|
|
|
|
$self->lmLog( "SMTP Subject " . $subject, 'debug' );
|
|
|
|
$self->lmLog( "SMTP Body " . $body, 'debug' );
|
|
|
|
$self->lmLog( "SMTP HTML flag " . ( $html ? "on" : "off" ), 'debug' );
|
2011-05-27 11:41:13 +02:00
|
|
|
$self->lmLog( "SMTP Reply-To " . $self->{mailReplyTo}, 'debug' )
|
|
|
|
if $self->{mailReplyTo};
|
|
|
|
|
2011-05-27 16:30:14 +02:00
|
|
|
# Encode the subject
|
2011-06-22 17:31:36 +02:00
|
|
|
$subject = encode_base64($subject);
|
|
|
|
$subject =~ s/\s//g;
|
|
|
|
$subject = "=?$charset?B?" . $subject . "?=";
|
2011-05-27 16:30:14 +02:00
|
|
|
|
2011-05-27 18:20:56 +02:00
|
|
|
# Detect included images (cid)
|
|
|
|
my %cid = ( $body =~ m/"cid:([^:]+):([^"]+)"/g );
|
|
|
|
|
|
|
|
# Replace cid in body
|
|
|
|
$body =~ s/"cid:([^:]+):([^"]+)"/"cid:$1"/g;
|
|
|
|
|
2009-12-21 23:28:38 +01:00
|
|
|
eval {
|
2011-05-27 18:20:56 +02:00
|
|
|
|
|
|
|
# Create message
|
2011-05-30 14:06:52 +02:00
|
|
|
my $message;
|
|
|
|
|
|
|
|
# HTML case
|
|
|
|
if ($html) {
|
|
|
|
$message = MIME::Lite->new(
|
|
|
|
From => $self->{mailFrom},
|
|
|
|
To => $mail,
|
|
|
|
"Reply-To" => $self->{mailReplyTo},
|
|
|
|
Subject => $subject,
|
|
|
|
Type => 'multipart/related',
|
|
|
|
);
|
|
|
|
|
|
|
|
# Attach HTML message
|
2011-05-27 18:20:56 +02:00
|
|
|
$message->attach(
|
2011-05-30 14:06:52 +02:00
|
|
|
Type => "text/html; charset=$charset",
|
|
|
|
Data => qq{$body},
|
|
|
|
);
|
|
|
|
|
|
|
|
# Attach included images
|
|
|
|
foreach ( keys %cid ) {
|
|
|
|
$message->attach(
|
|
|
|
Type => "image/" . ( $cid{$_} =~ m/\.(\w+)/ )[0],
|
|
|
|
Id => $_,
|
|
|
|
Path => $self->getApacheHtdocsPath() . "/" . $cid{$_},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Plain text case
|
|
|
|
else {
|
|
|
|
$message = MIME::Lite->new(
|
|
|
|
From => $self->{mailFrom},
|
|
|
|
To => $mail,
|
|
|
|
"Reply-To" => $self->{mailReplyTo},
|
|
|
|
Subject => $subject,
|
|
|
|
Type => 'TEXT',
|
|
|
|
Data => $body,
|
2011-05-27 18:20:56 +02:00
|
|
|
);
|
2011-05-30 14:06:52 +02:00
|
|
|
|
|
|
|
# Manage content type and charset
|
|
|
|
$message->attr( "content-type" => "text/plain" );
|
|
|
|
$message->attr( "content-type.charset" => $charset );
|
|
|
|
|
2011-05-27 18:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Send the mail
|
2009-12-21 23:28:38 +01:00
|
|
|
$self->{SMTPServer}
|
2011-04-14 17:59:42 +02:00
|
|
|
? $message->send(
|
|
|
|
"smtp", $self->{SMTPServer},
|
|
|
|
AuthUser => $self->{SMTPAuthUser},
|
|
|
|
AuthPass => $self->{SMTPAuthPass}
|
|
|
|
)
|
2009-12-21 23:28:38 +01:00
|
|
|
: $message->send();
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
$self->lmLog( "Send message failed: $@", 'error' );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-05-20 16:30:21 +02:00
|
|
|
## @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 {
|
2016-01-02 10:29:05 +01:00
|
|
|
my ( $self, $user ) = @_;
|
2011-05-20 16:30:21 +02:00
|
|
|
|
2014-02-26 11:57:49 +01:00
|
|
|
my $moduleOptions = $self->{globalStorageOptions} || {};
|
|
|
|
$moduleOptions->{backend} = $self->{globalStorage};
|
|
|
|
my $module = "Lemonldap::NG::Common::Apache::Session";
|
|
|
|
|
2011-05-20 16:30:21 +02:00
|
|
|
# Search on mail sessions
|
2014-02-26 11:57:49 +01:00
|
|
|
my $sessions = $module->searchOn( $moduleOptions, "user", $user );
|
2011-05-20 16:30:21 +02:00
|
|
|
|
|
|
|
# Browse found sessions to check if it's a mail session
|
|
|
|
foreach my $id ( keys %$sessions ) {
|
2014-02-26 11:57:49 +01:00
|
|
|
my $mailSession = $self->getApacheSession( $id, 1 );
|
2014-10-27 12:19:25 +01:00
|
|
|
next unless ($mailSession);
|
2014-02-26 11:57:49 +01:00
|
|
|
return $id if ( $mailSession->data->{_type} =~ /^mail$/ );
|
2011-05-20 16:30:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# 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 {
|
2016-01-02 10:29:05 +01:00
|
|
|
my ( $self, $mail ) = @_;
|
2014-05-23 20:47:36 +02:00
|
|
|
|
|
|
|
my $moduleOptions = $self->{globalStorageOptions} || {};
|
|
|
|
$moduleOptions->{backend} = $self->{globalStorage};
|
|
|
|
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 ) {
|
|
|
|
my $registerSession = $self->getApacheSession( $id, 1 );
|
2014-10-27 12:19:25 +01:00
|
|
|
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 "";
|
|
|
|
}
|
|
|
|
|
2009-12-21 23:28:38 +01:00
|
|
|
1;
|