Skip to content

Commit

Permalink
Fix: Ensure e-mails would always have a sender address set
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-rueegg committed Aug 17, 2023
1 parent 82450b0 commit 7b19fbe
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 11 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG-DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ HumHub Changelog

1.16.0 (Unreleased)
-------------------
- Enh #6451: Introduce Archiveable, Deletable, Editable, Readable, & Viewable Interfaces
- Enh #6512: Show error messages when DB connection configuration is invalid
- Fix #6519: Ensure e-mails would always have a sender address set
- Fix #6516: Humhub test case would fail on skipped tests
- Enh #6512: Show error messages when DB connection configuration is invalid
- Enh #6451: Introduce Archiveable, Deletable, Editable, Readable, & Viewable Interfaces

1.15.0-beta.2 (Unreleased)
--------------------------
Expand Down
5 changes: 5 additions & 0 deletions protected/humhub/components/ApplicationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ public function setHomeUrl($value)
{
$this->_homeUrl = $value;
}

public function getMailer(): MailerInterface
{
return parent::getMailer();
}
}
31 changes: 23 additions & 8 deletions protected/humhub/components/mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

namespace humhub\components\mail;

use humhub\interfaces\MailerInterface;
use Symfony\Component\Mime\Crypto\SMimeSigner;
use Yii;
use yii\mail\MessageInterface;

/**
* Mailer implements a mailer based on SymfonyMailer.
Expand All @@ -18,7 +20,7 @@
* @since 1.2
* @author Luke
*/
class Mailer extends \yii\symfonymailer\Mailer
class Mailer extends \yii\symfonymailer\Mailer implements MailerInterface
{
/**
* @inheritdoc
Expand Down Expand Up @@ -68,13 +70,7 @@ public function compose($view = null, array $params = [])
{
$message = parent::compose($view, $params);

// Set HumHub default from values
if (empty($message->getFrom())) {
$message->setFrom([Yii::$app->settings->get('mailer.systemEmailAddress') => Yii::$app->settings->get('mailer.systemEmailName')]);
if ($replyTo = Yii::$app->settings->get('mailer.systemEmailReplyTo')) {
$message->setReplyTo($replyTo);
}
}
self::ensureHumHubDefaultFromValues($message);

if ($this->signingCertificatePath !== null && $this->signingPrivateKeyPath !== null) {
if ($this->signer === null) {
Expand All @@ -92,6 +88,25 @@ public function compose($view = null, array $params = [])
return $message;
}

/**
* @param MessageInterface $message
*
* @return void
*/
public static function ensureHumHubDefaultFromValues(MessageInterface $message): MessageInterface
{
// Set HumHub default from values
if ($message->getFrom()) {
return $message;
}

$message->setFrom([Yii::$app->settings->get('mailer.systemEmailAddress') => Yii::$app->settings->get('mailer.systemEmailName')]);
if ($replyTo = Yii::$app->settings->get('mailer.systemEmailReplyTo')) {
$message->setReplyTo($replyTo);
}

return $message;
}

/**
* @inheritdoc
Expand Down
2 changes: 2 additions & 0 deletions protected/humhub/interfaces/ApplicationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ interface ApplicationInterface
* @event ActionEvent an event raised on init of application.
*/
public const EVENT_ON_INIT = 'onInit';

public function getMailer(): MailerInterface;
}
13 changes: 13 additions & 0 deletions protected/humhub/interfaces/MailerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/*
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace humhub\interfaces;

interface MailerInterface extends \yii\mail\MailerInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public function handle(BaseNotification $notification, User $recipient)
], $notification->getViewParams());

$mail = Yii::$app->mailer->compose($this->view, $viewParams)
->setFrom([Yii::$app->settings->get('mailer.systemEmailAddress') => Yii::$app->settings->get('mailer.systemEmailName')])
->setTo($recipient->email)
->setSubject(str_replace("\n", " ", trim($notification->getMailSubject())));
if ($replyTo = Yii::$app->settings->get('mailer.systemEmailReplyTo')) {
Expand Down
25 changes: 25 additions & 0 deletions protected/humhub/tests/codeception/_support/TestMailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace tests\codeception\_support;

use humhub\components\mail\Mailer;
use humhub\interfaces\MailerInterface;

/**
* @since 1.15
*/
class TestMailer extends \Codeception\Lib\Connector\Yii2\TestMailer implements MailerInterface
{
public function compose($view = null, array $params = [])
{
$message = parent::compose($view, $params);

return Mailer::ensureHumHubDefaultFromValues($message);
}
}
8 changes: 8 additions & 0 deletions protected/humhub/tests/codeception/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/**
* Application configuration shared by all test types
*/

$default = [
'name' => 'HumHub Test',
'language' => 'en-US',
Expand Down Expand Up @@ -32,6 +33,13 @@
'scriptUrl' => '/index-test.php',
],
],
'container' => [
'definitions' => [
\Codeception\Lib\Connector\Yii2\TestMailer::class => [
'class' => \tests\codeception\_support\TestMailer::class,
]
]
],
'modules' => [
'user' => [
'passwordStrength' => [
Expand Down

0 comments on commit 7b19fbe

Please sign in to comment.