Skip to content

Commit 431c6d0

Browse files
joshtrichardssusnux
authored andcommitted
fix(Mailer): Fix sendmail binary fallback
Signed-off-by: Josh <[email protected]>
1 parent bfa0888 commit 431c6d0

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace OCA\Settings\Settings\Admin;
77

88
use OCP\AppFramework\Http\TemplateResponse;
9+
use OCP\IBinaryFinder;
910
use OCP\IConfig;
1011
use OCP\IL10N;
1112
use OCP\Settings\IDelegatedSettings;
@@ -30,9 +31,11 @@ public function __construct(IConfig $config, IL10N $l) {
3031
* @return TemplateResponse
3132
*/
3233
public function getForm() {
34+
$finder = \OCP\Server::get(IBinaryFinder::class);
35+
3336
$parameters = [
3437
// Mail
35-
'sendmail_is_available' => (bool) \OC_Helper::findBinaryPath('sendmail'),
38+
'sendmail_is_available' => $finder->findBinaryPath('sendmail') !== false,
3639
'mail_domain' => $this->config->getSystemValue('mail_domain', ''),
3740
'mail_from_address' => $this->config->getSystemValue('mail_from_address', ''),
3841
'mail_smtpmode' => $this->config->getSystemValue('mail_smtpmode', ''),

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77

88
use OCA\Settings\Settings\Admin\Mail;
99
use OCP\AppFramework\Http\TemplateResponse;
10+
use OCP\IBinaryFinder;
1011
use OCP\IConfig;
1112
use OCP\IL10N;
13+
use PHPUnit\Framework\MockObject\MockObject;
1214
use Test\TestCase;
1315

1416
class MailTest extends TestCase {
15-
/** @var Mail */
16-
private $admin;
17-
/** @var IConfig */
18-
private $config;
19-
/** @var IL10N */
20-
private $l10n;
17+
18+
private Mail $admin;
19+
private IConfig&MockObject $config;
20+
private IL10N&MockObject $l10n;
2121

2222
protected function setUp(): void {
2323
parent::setUp();
@@ -30,7 +30,22 @@ protected function setUp(): void {
3030
);
3131
}
3232

33-
public function testGetForm() {
33+
public static function dataGetForm(): array {
34+
return [
35+
[true],
36+
[false],
37+
];
38+
}
39+
40+
/** @dataProvider dataGetForm */
41+
public function testGetForm(bool $sendmail) {
42+
$finder = $this->createMock(IBinaryFinder::class);
43+
$finder->expects(self::once())
44+
->method('findBinaryPath')
45+
->with('sendmail')
46+
->willReturn($sendmail ? '/usr/bin/sendmail': false);
47+
$this->overwriteService(IBinaryFinder::class, $finder);
48+
3449
$this->config
3550
->expects($this->any())
3651
->method('getSystemValue')
@@ -51,7 +66,7 @@ public function testGetForm() {
5166
'settings',
5267
'settings/admin/additional-mail',
5368
[
54-
'sendmail_is_available' => (bool) \OC_Helper::findBinaryPath('sendmail'),
69+
'sendmail_is_available' => $sendmail,
5570
'mail_domain' => 'mx.nextcloud.com',
5671
'mail_from_address' => '[email protected]',
5772
'mail_smtpmode' => 'smtp',

lib/private/Mail/Mailer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,10 @@ protected function getSendMailInstance(): SendmailTransport {
334334
break;
335335
default:
336336
$sendmail = \OCP\Server::get(IBinaryFinder::class)->findBinaryPath('sendmail');
337-
if ($sendmail === null) {
337+
if ($sendmail === false) {
338+
// fallback (though not sure what good it'll do)
338339
$sendmail = '/usr/sbin/sendmail';
340+
$this->logger->debug('sendmail binary search failed, using fallback ' . $sendmail, ['app' => 'core']);
339341
}
340342
$binaryPath = $sendmail;
341343
break;
@@ -346,6 +348,7 @@ protected function getSendMailInstance(): SendmailTransport {
346348
default => ' -bs',
347349
};
348350

351+
$this->logger->debug('Using sendmail binary: ' . $binaryPath, ['app' => 'core']);
349352
return new SendmailTransport($binaryPath . $binaryParam, null, $this->logger);
350353
}
351354
}

tests/lib/Mail/MailerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OC\Mail\Message;
1313
use OCP\Defaults;
1414
use OCP\EventDispatcher\IEventDispatcher;
15+
use OCP\IBinaryFinder;
1516
use OCP\IConfig;
1617
use OCP\IL10N;
1718
use OCP\IURLGenerator;
@@ -86,7 +87,7 @@ public function testGetSendmailInstanceSendMail($sendmailMode, $binaryParam) {
8687
['mail_sendmailmode', 'smtp', $sendmailMode],
8788
]);
8889

89-
$path = \OC_Helper::findBinaryPath('sendmail');
90+
$path = \OCP\Server::get(IBinaryFinder::class)->findBinaryPath('sendmail');
9091
if ($path === false) {
9192
$path = '/usr/sbin/sendmail';
9293
}

0 commit comments

Comments
 (0)