From a16aa995866d3319d9f848e0d5811786fad2f223 Mon Sep 17 00:00:00 2001 From: Arne Hamann Date: Sat, 16 Mar 2019 02:29:03 +0100 Subject: Added Hook before Message is send Signed-off-by: Arne Hamann --- lib/private/Mail/Mailer.php | 14 +++++++++++++- lib/private/Mail/Message.php | 17 +++++++++++++---- lib/private/Server.php | 3 ++- 3 files changed, 28 insertions(+), 6 deletions(-) (limited to 'lib/private') diff --git a/lib/private/Mail/Mailer.php b/lib/private/Mail/Mailer.php index df5f2687daa..47a7f8a7c9d 100644 --- a/lib/private/Mail/Mailer.php +++ b/lib/private/Mail/Mailer.php @@ -12,6 +12,7 @@ declare(strict_types=1); * @author Lukas Reschke * @author Morris Jobke * @author Roeland Jago Douma + * @author Arne Hamann * * @license AGPL-3.0 * @@ -34,6 +35,7 @@ namespace OC\Mail; use Egulias\EmailValidator\EmailValidator; use Egulias\EmailValidator\Validation\RFCValidation; use OCP\Defaults; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IL10N; use OCP\ILogger; @@ -42,6 +44,8 @@ use OCP\Mail\IAttachment; use OCP\Mail\IEMailTemplate; use OCP\Mail\IMailer; use OCP\Mail\IMessage; +use OCP\Mail\Events\BeforeMessageSent; + /** * Class Mailer provides some basic functions to create a mail message that can be used in combination with @@ -74,6 +78,8 @@ class Mailer implements IMailer { private $urlGenerator; /** @var IL10N */ private $l10n; + /** @var IEventDispatcher */ + private $dispatcher; /** * @param IConfig $config @@ -81,17 +87,20 @@ class Mailer implements IMailer { * @param Defaults $defaults * @param IURLGenerator $urlGenerator * @param IL10N $l10n + * @param IEventDispatcher $dispatcher */ public function __construct(IConfig $config, ILogger $logger, Defaults $defaults, IURLGenerator $urlGenerator, - IL10N $l10n) { + IL10N $l10n, + IEventDispatcher $dispatcher) { $this->config = $config; $this->logger = $logger; $this->defaults = $defaults; $this->urlGenerator = $urlGenerator; $this->l10n = $l10n; + $this->dispatcher = $dispatcher; } /** @@ -182,6 +191,9 @@ class Mailer implements IMailer { $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger)); } + + $this->dispatcher->dispatchTyped(new BeforeMessageSent($message)); + $mailer->send($message->getSwiftMessage(), $failedRecipients); // Debugging logging diff --git a/lib/private/Mail/Message.php b/lib/private/Mail/Message.php index 4ea9da532b6..9437bd0216f 100644 --- a/lib/private/Mail/Message.php +++ b/lib/private/Mail/Message.php @@ -11,6 +11,7 @@ declare(strict_types=1); * @author Morris Jobke * @author Roeland Jago Douma * @author Thomas Müller + * @author Arne Hamann * * @license AGPL-3.0 * @@ -112,7 +113,7 @@ class Message implements IMessage { * @return array */ public function getFrom(): array { - return $this->swiftMessage->getFrom(); + return $this->swiftMessage->getFrom() ?? []; } /** @@ -156,7 +157,7 @@ class Message implements IMessage { * @return array */ public function getTo(): array { - return $this->swiftMessage->getTo(); + return $this->swiftMessage->getTo() ?? []; } /** @@ -178,7 +179,7 @@ class Message implements IMessage { * @return array */ public function getCc(): array { - return $this->swiftMessage->getCc(); + return $this->swiftMessage->getCc() ?? []; } /** @@ -200,7 +201,7 @@ class Message implements IMessage { * @return array */ public function getBcc(): array { - return $this->swiftMessage->getBcc(); + return $this->swiftMessage->getBcc() ?? []; } /** @@ -256,6 +257,14 @@ class Message implements IMessage { return $this; } + /** + * Get's the underlying SwiftMessage + * @param Swift_Message $swiftMessage + */ + public function setSwiftMessage(Swift_Message $swiftMessage): void { + $this->swiftMessage = $swiftMessage; + } + /** * Get's the underlying SwiftMessage * @return Swift_Message diff --git a/lib/private/Server.php b/lib/private/Server.php index 3300920edb7..971b144e1d0 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -988,7 +988,8 @@ class Server extends ServerContainer implements IServerContainer { $c->getLogger(), $c->query(Defaults::class), $c->getURLGenerator(), - $c->getL10N('lib') + $c->getL10N('lib'), + $c->query(IEventDispatcher::class) ); }); $this->registerDeprecatedAlias('Mailer', IMailer::class); -- cgit v1.2.3