Skip to content

Commit f8690a6

Browse files
joshtrichardssusnux
authored andcommitted
fix(Mailer): Fix sendmail binary fallback
Signed-off-by: Josh <josh.t.richards@gmail.com>
1 parent 27203bb commit f8690a6

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

apps/settings/lib/Settings/Admin/Mail.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
namespace OCA\Settings\Settings\Admin;
2929

3030
use OCP\AppFramework\Http\TemplateResponse;
31+
use OCP\IBinaryFinder;
3132
use OCP\IConfig;
3233
use OCP\IL10N;
3334
use OCP\Settings\IDelegatedSettings;
@@ -52,9 +53,11 @@ public function __construct(IConfig $config, IL10N $l) {
5253
* @return TemplateResponse
5354
*/
5455
public function getForm() {
56+
$finder = \OCP\Server::get(IBinaryFinder::class);
57+
5558
$parameters = [
5659
// Mail
57-
'sendmail_is_available' => (bool) \OC_Helper::findBinaryPath('sendmail'),
60+
'sendmail_is_available' => $finder->findBinaryPath('sendmail') !== false,
5861
'mail_domain' => $this->config->getSystemValue('mail_domain', ''),
5962
'mail_from_address' => $this->config->getSystemValue('mail_from_address', ''),
6063
'mail_smtpmode' => $this->config->getSystemValue('mail_smtpmode', ''),

apps/settings/tests/Settings/Admin/MailTest.php

+23-8
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@
3030

3131
use OCA\Settings\Settings\Admin\Mail;
3232
use OCP\AppFramework\Http\TemplateResponse;
33+
use OCP\IBinaryFinder;
3334
use OCP\IConfig;
3435
use OCP\IL10N;
36+
use PHPUnit\Framework\MockObject\MockObject;
3537
use Test\TestCase;
3638

3739
class MailTest extends TestCase {
38-
/** @var Mail */
39-
private $admin;
40-
/** @var IConfig */
41-
private $config;
42-
/** @var IL10N */
43-
private $l10n;
40+
41+
private Mail $admin;
42+
private IConfig|MockObject $config;
43+
private IL10N|MockObject $l10n;
4444

4545
protected function setUp(): void {
4646
parent::setUp();
@@ -53,7 +53,22 @@ protected function setUp(): void {
5353
);
5454
}
5555

56-
public function testGetForm() {
56+
public static function dataGetForm(): array {
57+
return [
58+
[true],
59+
[false],
60+
];
61+
}
62+
63+
/** @dataProvider dataGetForm */
64+
public function testGetForm(bool $sendmail) {
65+
$finder = $this->createMock(IBinaryFinder::class);
66+
$finder->expects(self::once())
67+
->method('findBinaryPath')
68+
->with('sendmail')
69+
->willReturn($sendmail ? '/usr/bin/sendmail': false);
70+
$this->overwriteService(IBinaryFinder::class, $finder);
71+
5772
$this->config
5873
->expects($this->any())
5974
->method('getSystemValue')
@@ -74,7 +89,7 @@ public function testGetForm() {
7489
'settings',
7590
'settings/admin/additional-mail',
7691
[
77-
'sendmail_is_available' => (bool) \OC_Helper::findBinaryPath('sendmail'),
92+
'sendmail_is_available' => $sendmail,
7893
'mail_domain' => 'mx.nextcloud.com',
7994
'mail_from_address' => 'no-reply@nextcloud.com',
8095
'mail_smtpmode' => 'smtp',

lib/private/Mail/Mailer.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,10 @@ protected function getSendMailInstance(): SendmailTransport {
361361
break;
362362
default:
363363
$sendmail = \OCP\Server::get(IBinaryFinder::class)->findBinaryPath('sendmail');
364-
if ($sendmail === null) {
364+
if ($sendmail === false) {
365+
// fallback (though not sure what good it'll do)
365366
$sendmail = '/usr/sbin/sendmail';
367+
$this->logger->debug('sendmail binary search failed, using fallback ' . $sendmail, ['app' => 'core']);
366368
}
367369
$binaryPath = $sendmail;
368370
break;
@@ -373,6 +375,7 @@ protected function getSendMailInstance(): SendmailTransport {
373375
default => ' -bs',
374376
};
375377

378+
$this->logger->debug('Using sendmail binary: ' . $binaryPath, ['app' => 'core']);
376379
return new SendmailTransport($binaryPath . $binaryParam, null, $this->logger);
377380
}
378381
}

tests/lib/Mail/MailerTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use OC\Mail\Message;
1717
use OCP\Defaults;
1818
use OCP\EventDispatcher\IEventDispatcher;
19+
use OCP\IBinaryFinder;
1920
use OCP\IConfig;
2021
use OCP\IL10N;
2122
use OCP\IURLGenerator;
@@ -90,7 +91,7 @@ public function testGetSendmailInstanceSendMail($sendmailMode, $binaryParam) {
9091
['mail_sendmailmode', 'smtp', $sendmailMode],
9192
]);
9293

93-
$path = \OC_Helper::findBinaryPath('sendmail');
94+
$path = \OCP\Server::get(IBinaryFinder::class)->findBinaryPath('sendmail');
9495
if ($path === false) {
9596
$path = '/usr/sbin/sendmail';
9697
}

0 commit comments

Comments
 (0)