Skip to content

Commit

Permalink
Merge pull request #10574 from nextcloud/bug/10066/warning-mail-trans…
Browse files Browse the repository at this point in the history
…port

feat: setup check for mail transport php-mail
  • Loading branch information
kesselb authored Jan 16, 2025
2 parents c8ac25c + d24a009 commit 0ef25c0
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
use OCA\Mail\Service\Search\MailSearch;
use OCA\Mail\Service\TrustedSenderService;
use OCA\Mail\Service\UserPreferenceService;
use OCA\Mail\SetupChecks\MailTransport;
use OCA\Mail\Vendor\Favicon\Favicon;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
Expand Down Expand Up @@ -158,6 +159,8 @@ public function register(IRegistrationContext $context): void {

$context->registerNotifierService(Notifier::class);

$context->registerSetupCheck(MailTransport::class);

// bypass Horde Translation system
Horde_Translation::setHandler('Horde_Imap_Client', new HordeTranslationHandler());
Horde_Translation::setHandler('Horde_Mime', new HordeTranslationHandler());
Expand Down
43 changes: 43 additions & 0 deletions lib/SetupChecks/MailTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Mail\SetupChecks;

use OCP\IConfig;
use OCP\IL10N;
use OCP\SetupCheck\ISetupCheck;
use OCP\SetupCheck\SetupResult;

class MailTransport implements ISetupCheck {
public function __construct(
private IConfig $config,
private IL10N $l10n,
) {
}

public function getName(): string {
return $this->l10n->t('Mail Transport configuration');
}

public function getCategory(): string {
return 'mail';
}

public function run(): SetupResult {
$transport = $this->config->getSystemValueString('app.mail.transport', 'smtp');

if ($transport === 'smtp') {
return SetupResult::success();
}

return SetupResult::warning(
$this->l10n->t('The app.mail.transport setting is not set to smtp. This configuration can cause issues with modern email security measures such as SPF and DKIM because emails are sent directly from the web server, which is often not properly configured for this purpose. To address this, we have discontinued support for the mail transport. Please remove app.mail.transport from your configuration to use the SMTP transport and hide this message. A properly configured SMTP setup is required to ensure email delivery.')
);
}
}
61 changes: 61 additions & 0 deletions tests/Unit/SetupChecks/MailTransportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Unit\SetupChecks;

use ChristophWurst\Nextcloud\Testing\TestCase;
use OC\L10N\L10N;
use OCA\Mail\SetupChecks\MailTransport;
use OCP\IConfig;
use OCP\IL10N;
use OCP\SetupCheck\SetupResult;
use PHPUnit\Framework\MockObject\MockObject;

class MailTransportTest extends TestCase {
private IConfig&MockObject $config;
private IL10N&MockObject $l10n;
private MailTransport $check;

protected function setUp(): void {
parent::setUp();

$this->config = $this->createMock(IConfig::class);
$this->l10n = $this->createMock(L10N::class);

$this->check = new MailTransport($this->config, $this->l10n);
}

public function testSuccess(): void {
$this->config->method('getSystemValueString')
->willReturn('smtp');

$result = $this->check->run();

$this->assertEquals(SetupResult::SUCCESS, $result->getSeverity());
}

public function testWarning(): void {
$this->config->method('getSystemValueString')
->willReturn('mail');

$result = $this->check->run();

$this->assertEquals(SetupResult::WARNING, $result->getSeverity());
}

public function testName(): void {
$this->l10n->method('t')
->willReturn('Translated Name');

$this->assertEquals('Translated Name', $this->check->getName());
}

public function testCategory(): void {
$this->assertEquals('mail', $this->check->getCategory());
}
}

0 comments on commit 0ef25c0

Please sign in to comment.