diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2024-11-22 09:59:14 +0000 |
---|---|---|
committer | Mario <mario@mariovavti.com> | 2024-11-22 09:59:14 +0000 |
commit | 3a2f787f968db498c29d3f9d1c933f04d6fe0a95 (patch) | |
tree | ebdd1631b08bc6f86014f247fc757db98f0002a9 | |
parent | 243a2a9bdf686fe46fec4e2214ed47c1571ce91c (diff) | |
download | volse-hubzilla-3a2f787f968db498c29d3f9d1c933f04d6fe0a95.tar.gz volse-hubzilla-3a2f787f968db498c29d3f9d1c933f04d6fe0a95.tar.bz2 volse-hubzilla-3a2f787f968db498c29d3f9d1c933f04d6fe0a95.zip |
Add Zotlabs\Lib\Mailer class to replace z_mail function.
-rw-r--r-- | Zotlabs/Lib/Mailer.php | 86 | ||||
-rw-r--r-- | include/network.php | 52 | ||||
-rw-r--r-- | tests/unit/Lib/MailerTest.php | 62 | ||||
-rw-r--r-- | tests/unit/UnitTestCase.php | 8 | ||||
-rw-r--r-- | vendor/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | vendor/composer/autoload_static.php | 1 |
6 files changed, 162 insertions, 48 deletions
diff --git a/Zotlabs/Lib/Mailer.php b/Zotlabs/Lib/Mailer.php new file mode 100644 index 000000000..ca2d84d0d --- /dev/null +++ b/Zotlabs/Lib/Mailer.php @@ -0,0 +1,86 @@ +<?php +/** + * Mailer class for sending emails from Hubzilla. + * + * SPDX-FileCopyrightText: 2024 Hubzilla Community + * SPDX-FileContributor: Harald Eilertsen + * + * SPDX-License-Identifier: MIT + */ + +namespace Zotlabs\Lib; + +use App; + +/** + * A class for sending emails. + * + * Based on the previous `z_mail` function, but adaped and made more + * robust and usable as a class. + */ +class Mailer { + + public function __construct(private array $params = []) { + } + + public function deliver(): bool { + + if(empty($this->params['fromEmail'])) { + $this->params['fromEmail'] = Config::Get('system','from_email'); + if(empty($this->params['fromEmail'])) { + $this->params['fromEmail'] = 'Administrator@' . App::get_hostname(); + } + } + + if(empty($this->params['fromName'])) { + $this->params['fromName'] = Config::Get('system','from_email_name'); + if(empty($this->params['fromName'])) { + $this->params['fromName'] = System::get_site_name(); + } + } + + if(empty($this->params['replyTo'])) { + $this->params['replyTo'] = Config::Get('system','reply_address'); + if(empty($this->params['replyTo'])) { + $this->params['replyTo'] = 'noreply@' . App::get_hostname(); + } + } + + if (!isset($this->params['additionalMailHeader'])) { + $this->params['additionalMailHeader'] = ''; + } + + $this->params['sent'] = false; + $this->params['result'] = false; + + /** + * @hooks email_send + * * \e params @see z_mail() + */ + call_hooks('email_send', $this->params); + + if($this->params['sent']) { + logger('notification: z_mail returns ' . (($this->params['result']) ? 'success' : 'failure'), LOGGER_DEBUG); + return $this->params['result']; + } + + $fromName = email_header_encode(html_entity_decode($this->params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8'); + $messageSubject = email_header_encode(html_entity_decode($this->params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8'); + + $messageHeader = + $this->params['additionalMailHeader'] . + "From: $fromName <{$this->params['fromEmail']}>" . PHP_EOL . + "Reply-To: $fromName <{$this->params['replyTo']}>" . PHP_EOL . + "Content-Type: text/plain; charset=UTF-8"; + + // send the message + $res = mail( + $this->params['toEmail'], // send to address + $messageSubject, // subject + $this->params['textVersion'], + $messageHeader // message headers + ); + logger('notification: z_mail returns ' . (($res) ? 'success' : 'failure'), LOGGER_DEBUG); + return $res; + } +} diff --git a/include/network.php b/include/network.php index 50e8b1c89..a8ccee15c 100644 --- a/include/network.php +++ b/include/network.php @@ -2,6 +2,7 @@ use Zotlabs\Lib\Activity; use Zotlabs\Lib\Config; +use Zotlabs\Lib\Mailer; use Zotlabs\Lib\Zotfinger; use Zotlabs\Lib\Libzot; use Zotlabs\Lib\Queue; @@ -1813,54 +1814,9 @@ function network_to_name($s) { */ function z_mail($params) { - if(! $params['fromEmail']) { - $params['fromEmail'] = Config::Get('system','from_email'); - if(! $params['fromEmail']) - $params['fromEmail'] = 'Administrator' . '@' . App::get_hostname(); - } - if(! $params['fromName']) { - $params['fromName'] = Config::Get('system','from_email_name'); - if(! $params['fromName']) - $params['fromName'] = Zotlabs\Lib\System::get_site_name(); - } - if(! $params['replyTo']) { - $params['replyTo'] = Config::Get('system','reply_address'); - if(! $params['replyTo']) - $params['replyTo'] = 'noreply' . '@' . App::get_hostname(); - } - - $params['sent'] = false; - $params['result'] = false; - - /** - * @hooks email_send - * * \e params @see z_mail() - */ - call_hooks('email_send', $params); - - if($params['sent']) { - logger('notification: z_mail returns ' . (($params['result']) ? 'success' : 'failure'), LOGGER_DEBUG); - return $params['result']; - } - - $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8'); - $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8'); - - $messageHeader = - $params['additionalMailHeader'] . - "From: $fromName <{$params['fromEmail']}>" . PHP_EOL . - "Reply-To: $fromName <{$params['replyTo']}>" . PHP_EOL . - "Content-Type: text/plain; charset=UTF-8"; - - // send the message - $res = mail( - $params['toEmail'], // send to address - $messageSubject, // subject - $params['textVersion'], - $messageHeader // message headers - ); - logger('notification: z_mail returns ' . (($res) ? 'success' : 'failure'), LOGGER_DEBUG); - return $res; + // Delegate the call to the Mailer class. + $mailer = new Mailer($params); + return $mailer->deliver(); } diff --git a/tests/unit/Lib/MailerTest.php b/tests/unit/Lib/MailerTest.php new file mode 100644 index 000000000..038c7ef4c --- /dev/null +++ b/tests/unit/Lib/MailerTest.php @@ -0,0 +1,62 @@ +<?php +/* + * Tests for the Zotlabs\LibMÌ€ailer class. + * + * SPDX-FileCopyrightText: 2024 Hubzilla Community + * SPDX-FileContributor: Harald Eilertsen + * + * SPDX-License-Identifier: MIT + */ + +namespace Zotlabs\Tests\Unit\Lib; + +use App; +use phpmock\phpunit\PHPMock; +use Zotlabs\Lib\Mailer; +use Zotlabs\Tests\Unit\UnitTestCase; + +class MailerTest extends UnitTestCase { + + use PHPMock; + + public function test_optional_params_replaced_by_defaults(): void { + $hostname = App::get_hostname(); + $recipient = 'recipient@somesite.test'; + $subject = 'A test email'; + $body = <<<EOF + Dear recipient, + + This is an test email message for you. + + Sincerely, + Hubzilla + EOF; + + // + // Catch calls to the php mail function, and verify + // that it is called with the args we're expecting + // + $this->getFunctionMock('Zotlabs\Lib', 'mail') + ->expects($this->once()) + ->with( + $this->identicalTo($recipient), + $this->identicalTo($subject), + $this->identicalTo($body), + $this->identicalTo(<<<EOF + From: <Administrator@{$hostname}> + Reply-To: <noreply@{$hostname}> + Content-Type: text/plain; charset=UTF-8 + EOF + ) + ) + ->willReturn(true); + + $mailer = new Mailer([ + 'toEmail' => $recipient, + 'messageSubject' => $subject, + 'textVersion' => $body, + ]); + + $mailer->deliver(); + } +} diff --git a/tests/unit/UnitTestCase.php b/tests/unit/UnitTestCase.php index 79f0ec440..e3cd22b63 100644 --- a/tests/unit/UnitTestCase.php +++ b/tests/unit/UnitTestCase.php @@ -75,6 +75,14 @@ class UnitTestCase extends TestCase { } /** + * Initialize the global App properties. + */ + #[Before] + protected function init_app(): void { + \App::set_hostname('hubzilla.test'); + } + + /** * Roll back test database to it's original state, cleaning up * any changes from the test. * diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index b19cd0f42..51fadd55a 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -1359,6 +1359,7 @@ return array( 'Zotlabs\\Lib\\Libsync' => $baseDir . '/Zotlabs/Lib/Libsync.php', 'Zotlabs\\Lib\\Libzot' => $baseDir . '/Zotlabs/Lib/Libzot.php', 'Zotlabs\\Lib\\Libzotdir' => $baseDir . '/Zotlabs/Lib/Libzotdir.php', + 'Zotlabs\\Lib\\Mailer' => $baseDir . '/Zotlabs/Lib/Mailer.php', 'Zotlabs\\Lib\\MarkdownSoap' => $baseDir . '/Zotlabs/Lib/MarkdownSoap.php', 'Zotlabs\\Lib\\MessageFilter' => $baseDir . '/Zotlabs/Lib/MessageFilter.php', 'Zotlabs\\Lib\\Multibase' => $baseDir . '/Zotlabs/Lib/Multibase.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 54e4d8470..d1c13098e 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -1611,6 +1611,7 @@ class ComposerStaticInit7b34d7e50a62201ec5d5e526a5b8b35d 'Zotlabs\\Lib\\Libsync' => __DIR__ . '/../..' . '/Zotlabs/Lib/Libsync.php', 'Zotlabs\\Lib\\Libzot' => __DIR__ . '/../..' . '/Zotlabs/Lib/Libzot.php', 'Zotlabs\\Lib\\Libzotdir' => __DIR__ . '/../..' . '/Zotlabs/Lib/Libzotdir.php', + 'Zotlabs\\Lib\\Mailer' => __DIR__ . '/../..' . '/Zotlabs/Lib/Mailer.php', 'Zotlabs\\Lib\\MarkdownSoap' => __DIR__ . '/../..' . '/Zotlabs/Lib/MarkdownSoap.php', 'Zotlabs\\Lib\\MessageFilter' => __DIR__ . '/../..' . '/Zotlabs/Lib/MessageFilter.php', 'Zotlabs\\Lib\\Multibase' => __DIR__ . '/../..' . '/Zotlabs/Lib/Multibase.php', |