new mail server options

This commit is contained in:
Roland Gruber 2020-03-10 21:08:20 +01:00
parent 0f40ba18c0
commit 50596a358e
5 changed files with 55 additions and 60 deletions

View File

@ -161,8 +161,6 @@ $helpArray = array (
"Text" => _("This is a list of IP addresses from hosts who may access LAM. You can use \"*\" as wildcard (e.g. 192.168.0.*).")), "Text" => _("This is a list of IP addresses from hosts who may access LAM. You can use \"*\" as wildcard (e.g. 192.168.0.*).")),
"242" => array ("Headline" => _("Password policy"), "242" => array ("Headline" => _("Password policy"),
"Text" => _("Here you can specify minimum requirements for passwords. The character classes are: lowercase, uppercase, numeric and symbols.")), "Text" => _("Here you can specify minimum requirements for passwords. The character classes are: lowercase, uppercase, numeric and symbols.")),
"243" => array ("Headline" => _('Email format'),
"Text" => _('Please change this setting only if you experience problems in receiving emails from LAM. This defines the line ending of emails.')),
"244" => array ("Headline" => _('PHP error reporting'), "244" => array ("Headline" => _('PHP error reporting'),
"Text" => _('Defines if the PHP error reporting setting from php.ini is used or the setting preferred by LAM ("E_ALL & ~E_NOTICE"). If you do not develop LAM modules please use the default. This will prevent displaying messages that are useful only for developers.')), "Text" => _('Defines if the PHP error reporting setting from php.ini is used or the setting preferred by LAM ("E_ALL & ~E_NOTICE"). If you do not develop LAM modules please use the default. This will prevent displaying messages that are useful only for developers.')),
"245" => array ("Headline" => _('Encrypt session'), "245" => array ("Headline" => _('Encrypt session'),

View File

@ -1282,9 +1282,7 @@ function sendPasswordMail($pwd, $user, $recipient = null) {
$body = str_replace('@@' . $attr . '@@', $value, $body); $body = str_replace('@@' . $attr . '@@', $value, $body);
$found = preg_match('/\@\@[^\@]+\@\@/', $body, $results); $found = preg_match('/\@\@[^\@]+\@\@/', $body, $results);
} }
$headerLines = createEMailHeaders($mailFrom, ($mailIsHTML == 'true'), $mailReplyTo); $success = sendEMail($mailTo, $subject, $body, $mailFrom, ($mailIsHTML == 'true'), $mailReplyTo);
$returnPath = empty($mailReplyTo) ? $mailFrom : $mailReplyTo;
$success = sendEMail($mailTo, $subject, $body, $headerLines, $returnPath);
if ($success) { if ($success) {
logNewMessage(LOG_DEBUG, 'Sent password mail to ' . $mailTo); logNewMessage(LOG_DEBUG, 'Sent password mail to ' . $mailTo);
return array( return array(
@ -1310,7 +1308,7 @@ function sendPasswordMail($pwd, $user, $recipient = null) {
* @return String header lines * @return String header lines
*/ */
function createEMailHeaders($from, $isHTML, $replyTo = null, $cc = null, $bcc = null) { function createEMailHeaders($from, $isHTML, $replyTo = null, $cc = null, $bcc = null) {
$headerLines = "X-Mailer: LDAP Account Manager\r\n"; $headerLines = "";
if (!empty($from)) { if (!empty($from)) {
$headerLines .= 'From: ' . encodeMailAddress($from) . "\r\n"; $headerLines .= 'From: ' . encodeMailAddress($from) . "\r\n";
} }
@ -1333,51 +1331,66 @@ function createEMailHeaders($from, $isHTML, $replyTo = null, $cc = null, $bcc =
return $headerLines; return $headerLines;
} }
/**
* Encodes the email address for the header part of an email.
*
* @param String $address email address
* @return String encoded mail address
*/
function encodeMailAddress($address) {
$matches = array();
// if the email contains a name part then base64 encode it
if (preg_match('/^(.*)<(.*)>$/', $address, $matches)) {
return base64EncodeForEMail($matches[1]) . ' <' . $matches[2] . '>';
}
return $address;
}
/**
* Returns a base64 encoded string of the given values in a format that is used in emails.
*
* @param String $value value to encode
* @return String base64 encoded value
*/
function base64EncodeForEMail($value) {
return '=?UTF-8?B?' . base64_encode($value) . '?=';
}
/** /**
* Sends out an email. * Sends out an email.
* *
* @param String $to TO address * @param String $to TO address
* @param String $subject email subject * @param String $subject email subject
* @param String $text mail body (with \r\n EOL) * @param String $text mail body (with \r\n EOL)
* @param String $headers header lines (with \r\n EOL) * @param String $from FROM address
* @param string $returnPath email to be used for return path * @param bool $isHTML HTML format
* @param String $replyTo REPLY-TO address (optional)
* @param String $cc CC address (optional)
* @param String $bcc BCC address (optional)
*/ */
function sendEMail($to, $subject, $text, $headers, $returnPath) { function sendEMail($to, $subject, $text, $from, $isHTML, $replyTo = null, $cc = null, $bcc = null) {
if (!empty($_SESSION['cfgMain']->mailEOL) && ($_SESSION['cfgMain']->mailEOL === 'unix')) { include_once __DIR__ . '/3rdParty/composer/autoload.php';
$text = str_replace("\r\n", "\n", $text); $returnPath = ($replyTo === null) ? $from : $replyTo;
$headers = str_replace("\r\n", "\n", $headers); $returnPathParsed = PHPMailer\PHPMailer\PHPMailer::parseAddresses($returnPath);
}
logNewMessage(LOG_DEBUG, "Send mail to $to\n" . $text); logNewMessage(LOG_DEBUG, "Send mail to $to\n" . $text);
$additionalParams = null; $mailer = new PHPMailer\PHPMailer\PHPMailer(true);
if (!empty($returnPath) && isCommandlineSafeEmailAddress($returnPath)) { try {
$additionalParams = '-f' . $returnPath; $cfgMain = $_SESSION['cfgMain'];
if (!empty($cfgMain->mailServer)) {
$mailer->isSMTP();
$serverParts = explode(':', $cfgMain->mailServer);
$mailer->Host = $serverParts[0];
$mailer->Port = $serverParts[1];
if (!empty($cfgMain->mailUser)) {
$mailer->SMTPAuth = true;
$mailer->Username = $cfgMain->mailUser;
$mailer->Password = $cfgMain->mailPassword;
$mailer->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
}
}
$mailer->CharSet = PHPMailer\PHPMailer\PHPMailer::CHARSET_UTF8;
$mailer->addAddress($to);
$mailer->Subject = $subject;
$mailer->Body = $text;
$mailer->Sender = $returnPathParsed[0]['address'];
$fromParsed = PHPMailer\PHPMailer\PHPMailer::parseAddresses($from);
$mailer->setFrom($fromParsed[0]['address'], $fromParsed[0]['name']);
$mailer->isHTML($isHTML);
if (!empty($replyTo)) {
$replyToParsed = PHPMailer\PHPMailer\PHPMailer::parseAddresses($replyTo);
$mailer->addReplyTo($replyToParsed[0]['address'], $replyToParsed[0]['name']);
}
if (!empty($cc)) {
$ccParsed = PHPMailer\PHPMailer\PHPMailer::parseAddresses($cc);
$mailer->addCC($ccParsed[0]['address'], $ccParsed[0]['name']);
}
if (!empty($bcc)) {
$bccParsed = PHPMailer\PHPMailer\PHPMailer::parseAddresses($bcc);
$mailer->addBCC($bccParsed[0]['address'], $bccParsed[0]['name']);
}
$mailer->XMailer = 'LDAP Account Manager';
$mailer->send();
return true;
}
catch (Exception $e) {
logNewMessage(LOG_ERR, 'Mail sending failed: ' . $e->getMessage());
return false;
} }
return mail($to, base64EncodeForEMail($subject), $text, $headers, $additionalParams);
} }
/** /**

View File

@ -2611,9 +2611,6 @@ class LAMCfgMain {
/** SSL certificate should be deleted on save() */ /** SSL certificate should be deleted on save() */
private $delSSLCaCert = false; private $delSSLCaCert = false;
/** EOL for emails (default/unix) */
public $mailEOL = 'default';
/** error reporting */ /** error reporting */
public $errorReporting = self::ERROR_REPORTING_DEFAULT; public $errorReporting = self::ERROR_REPORTING_DEFAULT;
@ -2636,7 +2633,7 @@ class LAMCfgMain {
"passwordMinClasses", "passwordMinSymbol", 'checkedRulesCount', "passwordMinClasses", "passwordMinSymbol", 'checkedRulesCount',
'passwordMustNotContainUser', 'passwordMustNotContain3Chars', 'passwordMustNotContainUser', 'passwordMustNotContain3Chars',
'externalPwdCheckUrl', 'externalPwdCheckUrl',
"mailEOL", 'errorReporting', 'encryptSession', 'allowedHostsSelfService', 'errorReporting', 'encryptSession', 'allowedHostsSelfService',
'license', 'mailServer', 'mailUser', 'mailPassword' 'license', 'mailServer', 'mailUser', 'mailPassword'
); );
@ -2780,9 +2777,6 @@ class LAMCfgMain {
if (!in_array("externalPwdCheckUrl", $saved)) { if (!in_array("externalPwdCheckUrl", $saved)) {
array_push($file_array, "\n\n" . "externalPwdCheckUrl: " . $this->externalPwdCheckUrl); array_push($file_array, "\n\n" . "externalPwdCheckUrl: " . $this->externalPwdCheckUrl);
} }
if (!in_array("mailEOL", $saved)) {
array_push($file_array, "\n\n# Email format (default/unix)\n" . "mailEOL: " . $this->mailEOL);
}
if (!in_array("errorReporting", $saved)) { if (!in_array("errorReporting", $saved)) {
array_push($file_array, "\n\n# PHP error reporting (default/system)\n" . "errorReporting: " . $this->errorReporting); array_push($file_array, "\n\n# PHP error reporting (default/system)\n" . "errorReporting: " . $this->errorReporting);
} }

View File

@ -245,7 +245,6 @@ if (isset($_POST['submitFormData'])) {
} }
// mail EOL // mail EOL
if (isLAMProVersion()) { if (isLAMProVersion()) {
$cfg->mailEOL = $_POST['mailEOL'];
$cfg->mailUser = $_POST['mailUser']; $cfg->mailUser = $_POST['mailUser'];
$cfg->mailPassword = $_POST['mailPassword']; $cfg->mailPassword = $_POST['mailPassword'];
$cfg->mailServer = $_POST['mailServer']; $cfg->mailServer = $_POST['mailServer'];
@ -468,13 +467,6 @@ printHeaderContents(_("Edit general settings"), '../..');
$mailPassword = new htmlResponsiveInputField(_("Password"), 'mailPassword', $cfg->mailPassword, '255'); $mailPassword = new htmlResponsiveInputField(_("Password"), 'mailPassword', $cfg->mailPassword, '255');
$mailPassword->setIsPassword(true); $mailPassword->setIsPassword(true);
$row->add($mailPassword, 12); $row->add($mailPassword, 12);
$mailEOLOptions = array(
_('Default (\r\n)') => 'default',
_('Non-standard (\n)') => 'unix'
);
$mailEOLSelect = new htmlResponsiveSelect('mailEOL', $mailEOLOptions, array($cfg->mailEOL), _('Email format'), '243');
$mailEOLSelect->setHasDescriptiveElements(true);
$row->add($mailEOLSelect, 12);
} }
$row->addVerticalSpacer('3rem'); $row->addVerticalSpacer('3rem');

View File

@ -28,7 +28,7 @@ include_once __DIR__ . '/../../lib/config.inc';
* *
* @author Roland Gruber * @author Roland Gruber
*/ */
class LAMConfigTest extends TestCase { class LAMCfgMainTest extends TestCase {
private $conf; private $conf;
private $file; private $file;
@ -59,7 +59,6 @@ class LAMConfigTest extends TestCase {
* Mail related settings * Mail related settings
*/ */
public function testMail() { public function testMail() {
$this->conf->mailEOL = 'unix123';
$this->conf->mailServer = 'server:123'; $this->conf->mailServer = 'server:123';
$this->conf->mailPassword = 'pwd123'; $this->conf->mailPassword = 'pwd123';
$this->conf->mailUser = 'user123'; $this->conf->mailUser = 'user123';
@ -67,7 +66,6 @@ class LAMConfigTest extends TestCase {
$this->conf->save(); $this->conf->save();
$this->conf = new LAMCfgMain($this->file); $this->conf = new LAMCfgMain($this->file);
$this->assertEquals('unix123', $this->conf->mailEOL);
$this->assertEquals('server:123', $this->conf->mailServer); $this->assertEquals('server:123', $this->conf->mailServer);
$this->assertEquals('pwd123', $this->conf->mailPassword); $this->assertEquals('pwd123', $this->conf->mailPassword);
$this->assertEquals('user123', $this->conf->mailUser); $this->assertEquals('user123', $this->conf->mailUser);