diff --git a/src/Ltb/AttributeValue.php b/src/Ltb/AttributeValue.php index c7f55f7..2b76e8a 100644 --- a/src/Ltb/AttributeValue.php +++ b/src/Ltb/AttributeValue.php @@ -58,11 +58,10 @@ public static function ldap_get_first_available_value($ldap, $entry, $attributes * Get from ldap entry first value corresponding to $mail_attributes (globally configured) * @param $ldap php_ldap connection object * @param $entry ldap entry to parse + * @param $mail_attributes array containing mail attributes * @return mail to use for notification or empty string if not found */ - public static function ldap_get_mail_for_notification($ldap, $entry) { - # mail_attibutes are set globally in configuration - global $mail_attributes; + public static function ldap_get_mail_for_notification($ldap, $entry, $mail_attributes) { $mailValue = \Ltb\AttributeValue::ldap_get_first_available_value($ldap, $entry, $mail_attributes); $mail=""; if ( $mailValue ) { diff --git a/src/Ltb/Mail.php b/src/Ltb/Mail.php index 01dd4bd..4a5eba3 100644 --- a/src/Ltb/Mail.php +++ b/src/Ltb/Mail.php @@ -4,74 +4,56 @@ use PHPMailer\PHPMailer\Exception; use PHPMailer\PHPMailer\SMTP; -final class Mail { +class Mail { - # Mail functions - # rely on global mail configured with $mail_XXXX variables + public $mailer = null; - # init $mailer from config $mail_XXXX variables - # legacy code to be compliant with self-service-password existing configuration - static function init_mailer() + # Mail functions + function __construct( $mail_priority, + $mail_charset, + $mail_contenttype, + $mail_wordwrap, + $mail_sendmailpath, + $mail_protocol, + $mail_smtp_debug, + $mail_debug_format, + $mail_smtp_host, + $mail_smtp_port, + $mail_smtp_secure, + $mail_smtp_autotls, + $mail_smtp_auth, + $mail_smtp_user, + $mail_smtp_pass, + $mail_smtp_keepalive, + $mail_smtp_options, + $mail_smtp_timeout + ) { - - #============================================================================== - # Email Config - #============================================================================== - - global $mailer; - global $mail_priority, $mail_charset, $mail_contenttype, $mail_wordwrap, $mail_sendmailpath; - global $mail_protocol, $mail_smtp_debug, $mail_debug_format, $mail_smtp_host, $mail_smtp_port; - global $mail_smtp_secure, $mail_smtp_autotls, $mail_smtp_auth, $mail_smtp_user, $mail_smtp_pass; - global $mail_smtp_keepalive, $mail_smtp_options, $mail_smtp_timeout; - - $mailer= new PHPMailer; - - $mailer->Priority = $mail_priority; - $mailer->CharSet = $mail_charset; - $mailer->ContentType = $mail_contenttype; - $mailer->WordWrap = $mail_wordwrap; - $mailer->Sendmail = $mail_sendmailpath; - $mailer->Mailer = $mail_protocol; - $mailer->SMTPDebug = $mail_smtp_debug; - $mailer->Debugoutput = $mail_debug_format; - $mailer->Host = $mail_smtp_host; - $mailer->Port = $mail_smtp_port; - $mailer->SMTPSecure = $mail_smtp_secure; - $mailer->SMTPAutoTLS = $mail_smtp_autotls; - $mailer->SMTPAuth = $mail_smtp_auth; - $mailer->Username = $mail_smtp_user; - $mailer->Password = $mail_smtp_pass; - $mailer->SMTPKeepAlive = $mail_smtp_keepalive; - $mailer->SMTPOptions = $mail_smtp_options; - $mailer->Timeout = $mail_smtp_timeout; - - return $mailer; + $this->mailer = new PHPMailer; + + $this->mailer->Priority = $mail_priority; + $this->mailer->CharSet = $mail_charset; + $this->mailer->ContentType = $mail_contenttype; + $this->mailer->WordWrap = $mail_wordwrap; + $this->mailer->Sendmail = $mail_sendmailpath; + $this->mailer->Mailer = $mail_protocol; + $this->mailer->SMTPDebug = $mail_smtp_debug; + $this->mailer->Debugoutput = $mail_debug_format; + $this->mailer->Host = $mail_smtp_host; + $this->mailer->Port = $mail_smtp_port; + $this->mailer->SMTPSecure = $mail_smtp_secure; + $this->mailer->SMTPAutoTLS = $mail_smtp_autotls; + $this->mailer->SMTPAuth = $mail_smtp_auth; + $this->mailer->Username = $mail_smtp_user; + $this->mailer->Password = $mail_smtp_pass; + $this->mailer->SMTPKeepAlive = $mail_smtp_keepalive; + $this->mailer->SMTPOptions = $mail_smtp_options; + $this->mailer->Timeout = $mail_smtp_timeout; + } - /* @function boolean send_mail_gloabl(PHPMailer $mailer, string $mail, string $mail_from, string $subject, string $body, array $data) + /* @function boolean send_mail(string $mail, string $mail_from, string $subject, string $body, array $data) * Send a mail, replace strings in body - * - * use global PHPMailer $mailer, create one from mail_XXX configurations if needed. - # - * @param mail Destination - * @param mail_from Sender - * @param subject Subject - * @param body Body - * @param data Data for string replacement - * @return result - */ - static function send_mail_global($mail, $mail_from, $mail_from_name, $subject, $body, $data) { - global $mailer; - if ( ! isset($mailer) ) - { - \Ltb\Mail::init_mailer(); - } - return \Ltb\Mail::send_mail($mailer, $mail, $mail_from, $mail_from_name, $subject, $body, $data); - } - - /* @function boolean send_mail(PHPMailer $mailer, string $mail, string $mail_from, string $subject, string $body, array $data) - * Send a mail, replace strings in body - * @param mailer PHPMailer object * @param mail Destination or array of destinations. * @param mail_from Sender * @param subject Subject @@ -79,11 +61,18 @@ static function send_mail_global($mail, $mail_from, $mail_from_name, $subject, $ * @param data Data for string replacement * @return result */ - static function send_mail($mailer, $mail, $mail_from, $mail_from_name, $subject, $body, $data) { + public function send_mail($mail, $mail_from, $mail_from_name, $subject, $body, $data) { $result = false; - if (!is_a($mailer, 'PHPMailer\PHPMailer\PHPMailer')) { + if( $this->mailer == null ) + { + error_log("send_mail: Mail object not initialized!"); + return $result; + } + + if( ! is_a(($this->mailer), 'PHPMailer\PHPMailer\PHPMailer') ) + { error_log("send_mail: PHPMailer object required!"); return $result; } @@ -103,25 +92,25 @@ static function send_mail($mailer, $mail, $mail_from, $mail_from_name, $subject, } # if not done addAddress and addReplyTo are cumulated at each call - $mailer->clearAddresses(); - $mailer->setFrom($mail_from, $mail_from_name); - $mailer->addReplyTo($mail_from, $mail_from_name); + $this->mailer->clearAddresses(); + $this->mailer->setFrom($mail_from, $mail_from_name); + $this->mailer->addReplyTo($mail_from, $mail_from_name); # support list of mails if ( is_array($mail) ) { foreach( $mail as $mailstr ) { - $mailer->addAddress($mailstr); + $this->mailer->addAddress($mailstr); } } else { - $mailer->addAddress($mail); + $this->mailer->addAddress($mail); } - $mailer->Subject = $subject; - $mailer->Body = $body; + $this->mailer->Subject = $subject; + $this->mailer->Body = $body; - $result = $mailer->send(); + $result = $this->mailer->send(); if (!$result) { - error_log("send_mail: ".$mailer->ErrorInfo); + error_log("send_mail: ".$this->mailer->ErrorInfo); } return $result; diff --git a/tests/Ltb/AttributeValueTest.php b/tests/Ltb/AttributeValueTest.php index c2b0209..4ce1a85 100644 --- a/tests/Ltb/AttributeValueTest.php +++ b/tests/Ltb/AttributeValueTest.php @@ -46,7 +46,7 @@ public function test_ldap_get_mail_for_notification(): void { // global variable for ldap_get_mail_for_notification function - $GLOBALS['mail_attributes'] = array("mail"); + $mail_attributes = array("mail"); $phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP'); $phpLDAPMock->shouldreceive([ @@ -59,7 +59,7 @@ public function test_ldap_get_mail_for_notification(): void ]); # Test ldap_get_mail_for_notification - $mail = Ltb\AttributeValue::ldap_get_mail_for_notification(null, null); + $mail = Ltb\AttributeValue::ldap_get_mail_for_notification(null, null, $mail_attributes); $this->assertEquals('test1@domain.com', $mail, "not getting test1@domain.com as mail for notification"); } @@ -67,7 +67,7 @@ public function test_ldap_get_proxy_for_notification(): void { // global variable for ldap_get_mail_for_notification function - $GLOBALS['mail_attributes'] = array("proxyAddresses"); + $mail_attributes = array("proxyAddresses"); $phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP'); $phpLDAPMock->shouldreceive([ @@ -80,7 +80,7 @@ public function test_ldap_get_proxy_for_notification(): void ]); # Test ldap_get_mail_for_notification - $mail = Ltb\AttributeValue::ldap_get_mail_for_notification(null, null); + $mail = Ltb\AttributeValue::ldap_get_mail_for_notification(null, null, $mail_attributes); $this->assertEquals('test1@domain.com', $mail, "not getting test1@domain.com as proxyAddress for notification"); } diff --git a/tests/Ltb/MailTest.php b/tests/Ltb/MailTest.php index ab8aece..ec8acde 100644 --- a/tests/Ltb/MailTest.php +++ b/tests/Ltb/MailTest.php @@ -2,59 +2,79 @@ require __DIR__ . '/../../vendor/autoload.php'; -$GLOBALS['mail_priority'] = 3; // Options: null (default), 1 = High, 3 = Normal, 5 = low. When null, the header is not set at all. -$GLOBALS['mail_charset'] = 'utf-8'; -$GLOBALS['mail_contenttype'] = 'text/plain'; -$GLOBALS['mail_wordwrap'] = 0; -$GLOBALS['mail_sendmailpath'] = '/usr/sbin/sendmail'; -$GLOBALS['mail_protocol'] = 'smtp'; -$GLOBALS['mail_smtp_debug'] = 0; -$GLOBALS['mail_debug_format'] = 'error_log'; -$GLOBALS['mail_smtp_host'] = '127.0.0.1'; -$GLOBALS['mail_smtp_port'] = '25'; -$GLOBALS['mail_smtp_secure'] = false; -$GLOBALS['mail_smtp_autotls'] = false; -$GLOBALS['mail_smtp_auth'] = false; -$GLOBALS['mail_smtp_user'] = ''; -$GLOBALS['mail_smtp_pass'] = ''; -$GLOBALS['mail_smtp_keepalive'] = false; -$GLOBALS['mail_smtp_options'] = array(); -$GLOBALS['mail_smtp_timeout'] = 30; - final class MailTest extends \Mockery\Adapter\Phpunit\MockeryTestCase { - public function test_init_mailer(): void + public function test_constructor(): void { - $mailer = Ltb\Mail::init_mailer(); - - $this->assertEquals($GLOBALS['mail_priority'], $mailer->Priority, "Error while setting mail_priority"); - $this->assertEquals($GLOBALS['mail_charset'], $mailer->CharSet, "Error while setting mail_charset"); - $this->assertEquals($GLOBALS['mail_contenttype'], $mailer->ContentType, "Error while setting mail_contenttype"); - $this->assertEquals($GLOBALS['mail_wordwrap'], $mailer->WordWrap, "Error while setting mail_wordwrap"); - $this->assertEquals($GLOBALS['mail_sendmailpath'], $mailer->Sendmail, "Error while setting mail_sendmailpath"); - $this->assertEquals($GLOBALS['mail_protocol'], $mailer->Mailer, "Error while setting mail_protocol"); - $this->assertEquals($GLOBALS['mail_smtp_debug'], $mailer->SMTPDebug, "Error while setting mail_smtp_debug"); - $this->assertEquals($GLOBALS['mail_debug_format'], $mailer->Debugoutput, "Error while setting mail_debug_format"); - $this->assertEquals($GLOBALS['mail_smtp_host'], $mailer->Host, "Error while setting mail_smtp_host"); - $this->assertEquals($GLOBALS['mail_smtp_port'], $mailer->Port, "Error while setting mail_smtp_port"); - $this->assertEquals($GLOBALS['mail_smtp_secure'], $mailer->SMTPSecure, "Error while setting mail_smtp_secure"); - $this->assertEquals($GLOBALS['mail_smtp_autotls'], $mailer->SMTPAutoTLS, "Error while setting mail_smtp_autotls"); - $this->assertEquals($GLOBALS['mail_smtp_auth'], $mailer->SMTPAuth, "Error while setting mail_smtp_auth"); - $this->assertEquals($GLOBALS['mail_smtp_user'], $mailer->Username, "Error while setting mail_smtp_user"); - $this->assertEquals($GLOBALS['mail_smtp_pass'], $mailer->Password, "Error while setting mail_smtp_pass"); - $this->assertEquals($GLOBALS['mail_smtp_keepalive'], $mailer->SMTPKeepAlive, "Error while setting mail_smtp_keepalive"); - $this->assertEquals($GLOBALS['mail_smtp_options'], $mailer->SMTPOptions, "Error while setting mail_smtp_options"); - $this->assertEquals($GLOBALS['mail_smtp_timeout'], $mailer->Timeout, "Error while setting mail_smtp_timeout"); + $mail_priority = 3; // Options: null (default), 1 = High, 3 = Normal, 5 = low. When null, the header is not set at all. + $mail_charset = 'utf-8'; + $mail_contenttype = 'text/plain'; + $mail_wordwrap = 0; + $mail_sendmailpath = '/usr/sbin/sendmail'; + $mail_protocol = 'smtp'; + $mail_smtp_debug = 0; + $mail_debug_format = 'error_log'; + $mail_smtp_host = '127.0.0.1'; + $mail_smtp_port = '25'; + $mail_smtp_secure = false; + $mail_smtp_autotls = false; + $mail_smtp_auth = false; + $mail_smtp_user = ''; + $mail_smtp_pass = ''; + $mail_smtp_keepalive = false; + $mail_smtp_options = array(); + $mail_smtp_timeout = 30; + + $mailer = new \Ltb\Mail( $mail_priority, + $mail_charset, + $mail_contenttype, + $mail_wordwrap, + $mail_sendmailpath, + $mail_protocol, + $mail_smtp_debug, + $mail_debug_format, + $mail_smtp_host, + $mail_smtp_port, + $mail_smtp_secure, + $mail_smtp_autotls, + $mail_smtp_auth, + $mail_smtp_user, + $mail_smtp_pass, + $mail_smtp_keepalive, + $mail_smtp_options, + $mail_smtp_timeout + ); + + $this->assertEquals($mail_priority, $mailer->mailer->Priority, "Error while setting mail_priority"); + $this->assertEquals($mail_charset, $mailer->mailer->CharSet, "Error while setting mail_charset"); + $this->assertEquals($mail_contenttype, $mailer->mailer->ContentType, "Error while setting mail_contenttype"); + $this->assertEquals($mail_wordwrap, $mailer->mailer->WordWrap, "Error while setting mail_wordwrap"); + $this->assertEquals($mail_sendmailpath, $mailer->mailer->Sendmail, "Error while setting mail_sendmailpath"); + $this->assertEquals($mail_protocol, $mailer->mailer->Mailer, "Error while setting mail_protocol"); + $this->assertEquals($mail_smtp_debug, $mailer->mailer->SMTPDebug, "Error while setting mail_smtp_debug"); + $this->assertEquals($mail_debug_format, $mailer->mailer->Debugoutput, "Error while setting mail_debug_format"); + $this->assertEquals($mail_smtp_host, $mailer->mailer->Host, "Error while setting mail_smtp_host"); + $this->assertEquals($mail_smtp_port, $mailer->mailer->Port, "Error while setting mail_smtp_port"); + $this->assertEquals($mail_smtp_secure, $mailer->mailer->SMTPSecure, "Error while setting mail_smtp_secure"); + $this->assertEquals($mail_smtp_autotls, $mailer->mailer->SMTPAutoTLS, "Error while setting mail_smtp_autotls"); + $this->assertEquals($mail_smtp_auth, $mailer->mailer->SMTPAuth, "Error while setting mail_smtp_auth"); + $this->assertEquals($mail_smtp_user, $mailer->mailer->Username, "Error while setting mail_smtp_user"); + $this->assertEquals($mail_smtp_pass, $mailer->mailer->Password, "Error while setting mail_smtp_pass"); + $this->assertEquals($mail_smtp_keepalive, $mailer->mailer->SMTPKeepAlive, "Error while setting mail_smtp_keepalive"); + $this->assertEquals($mail_smtp_options, $mailer->mailer->SMTPOptions, "Error while setting mail_smtp_options"); + $this->assertEquals($mail_smtp_timeout, $mailer->mailer->Timeout, "Error while setting mail_smtp_timeout"); } public function test_send_mail(): void { + $mailer = new \Ltb\Mail(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ); + $mail_from = "{mail_from}"; $mail_from_name = "ltb admin sender"; $mail_signature = ""; @@ -88,15 +108,20 @@ public function test_send_mail(): void $mailerMock->shouldreceive('send') ->andReturn(true); - $result = Ltb\Mail::send_mail($mailerMock, $mail, $mail_from, $mail_from_name, $subject, $body, $data); + $mailer->mailer = $mailerMock; + + $result = $mailer->send_mail($mail, $mail_from, $mail_from_name, $subject, $body, $data); $this->assertEquals('Mail test to ltbtest', $mailerMock->Subject, "Error while processing subject"); $this->assertEquals('Hello ltbtest, this is a mail test from ltbadminsender@example.com. Your new password is secret', $mailerMock->Body, "Error while processing body"); + $this->assertNotFalse($result, "Error in send() result"); } - public function test_send_mail_global(): void + public function test_send_mail_not_initialized(): void { + $mailer = new \Ltb\Mail(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ); + $mail_from = "{mail_from}"; $mail_from_name = "ltb admin sender"; $mail_signature = ""; @@ -110,30 +135,36 @@ public function test_send_mail_global(): void $subject = 'Mail test to {login}'; $body = 'Hello {login}, this is a mail test from {mail_from}. Your new password is {password}'; - $GLOBALS['mailer'] = Mockery::mock('PHPMailer\PHPMailer\PHPMailer'); + $mailer->mailer = null; - $GLOBALS['mailer']->shouldreceive('clearAddresses') - ->andReturn(true); + $result = $mailer->send_mail($mail, $mail_from, $mail_from_name, $subject, $body, $data); - $GLOBALS['mailer']->shouldreceive('setFrom') - ->with('ltbadminsender@example.com', 'ltb admin sender') - ->andReturn(true); + $this->assertFalse($result, "Unexpected 'not false' send() result"); + } - $GLOBALS['mailer']->shouldreceive('addReplyTo') - ->with('ltbadminsender@example.com', 'ltb admin sender') - ->andReturn(true); + public function test_send_mail_wrong_initialized(): void + { - $GLOBALS['mailer']->shouldreceive('addAddress') - ->with(Mockery::anyOf('ltbtest@domain.com', 'ltbadmin@domain.com')) - ->andReturn(true); + $mailer = new \Ltb\Mail(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ); - $GLOBALS['mailer']->shouldreceive('send') - ->andReturn(true); + $mail_from = "{mail_from}"; + $mail_from_name = "ltb admin sender"; + $mail_signature = ""; + $mail = ['{mail_to}','ltbadmin@domain.com']; + $data = [ + 'mail_from' => 'ltbadminsender@example.com', + "login" => 'ltbtest', + "mail_to" => 'ltbtest@domain.com', + "password" => 'secret' + ]; + $subject = 'Mail test to {login}'; + $body = 'Hello {login}, this is a mail test from {mail_from}. Your new password is {password}'; + + $mailer->mailer = "wrong"; - $result = Ltb\Mail::send_mail_global($mail, $mail_from, $mail_from_name, $subject, $body, $data); + $result = $mailer->send_mail($mail, $mail_from, $mail_from_name, $subject, $body, $data); - $this->assertEquals('Mail test to ltbtest', $GLOBALS['mailer']->Subject, "Error while processing subject"); - $this->assertEquals('Hello ltbtest, this is a mail test from ltbadminsender@example.com. Your new password is secret', $GLOBALS['mailer']->Body, "Error while processing body"); + $this->assertFalse($result, "Unexpected 'not false' send() result"); } }