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;
|
|
|
|
|
2010-01-21 18:38:55 +01:00
|
|
|
our $VERSION = '0.2';
|
2009-12-21 23:28:38 +01:00
|
|
|
|
|
|
|
## @method string gen_password()
|
|
|
|
# Generate a complex password based on a regular expression
|
|
|
|
# @param regexp regular expression
|
|
|
|
# @return complex password
|
|
|
|
sub gen_password {
|
|
|
|
my $self = shift;
|
|
|
|
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-01-21 18:38:55 +01:00
|
|
|
## @method int send_mail()
|
|
|
|
# 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 {
|
2009-12-21 23:28:38 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $mail = shift;
|
2010-01-22 12:25:37 +01:00
|
|
|
my $subject = shift;
|
|
|
|
my $body = shift;
|
|
|
|
my $html = shift;
|
2009-12-21 23:28:38 +01:00
|
|
|
|
|
|
|
$self->lmLog( "SMTP From " . $self->{mailFrom}, 'debug' );
|
|
|
|
$self->lmLog( "SMTP To " . $mail, 'debug' );
|
2010-01-22 12:25:37 +01:00
|
|
|
$self->lmLog( "SMTP Subject " . $subject, 'debug' );
|
|
|
|
$self->lmLog( "SMTP Body " . $body, 'debug' );
|
|
|
|
$self->lmLog( "SMTP HTML flag " . ($html?"on":"off"), 'debug' );
|
2009-12-21 23:28:38 +01:00
|
|
|
eval {
|
|
|
|
my $message = MIME::Lite->new(
|
|
|
|
From => $self->{mailFrom},
|
|
|
|
To => $mail,
|
2010-01-22 12:25:37 +01:00
|
|
|
Subject => $subject,
|
2009-12-21 23:28:38 +01:00
|
|
|
Type => "TEXT",
|
2010-01-22 12:25:37 +01:00
|
|
|
Data => $body,
|
2009-12-21 23:28:38 +01:00
|
|
|
);
|
2010-01-22 12:25:37 +01:00
|
|
|
$message->attr("content-type" => "text/html; charset=utf-8") if $html;
|
2009-12-21 23:28:38 +01:00
|
|
|
$self->{SMTPServer}
|
|
|
|
? $message->send( "smtp", $self->{SMTPServer} )
|
|
|
|
: $message->send();
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
$self->lmLog( "Send message failed: $@", 'error' );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|