\n";
+ $this->headers .= "List-Archive: <" . $baseUrl->get() . "/notifications/system>\n";
+ }
+
+ /**
+ * Gets the subject of the concrete builder, which inherits this base class
+ *
+ * @return string
+ */
+ abstract protected function getSubject();
+
+ /**
+ * Gets the HTML version of the body of the concrete builder, which inherits this base class
+ *
+ * @return string
+ */
+ abstract protected function getHtmlMessage();
+
+ /**
+ * Gets the Plaintext version of the body of the concrete builder, which inherits this base class
+ *
+ * @return string
+ */
+ abstract protected function getPlaintextMessage();
+
+ /**
+ * Adds the User ID to the email in case the mail sending needs additional properties of this user
+ *
+ * @param array $user The user entity/array, for which the email should be sent
+ *
+ * @return static
+ * @todo Once the user array is replaced with a user entity, replace this array parameter as well
+ */
+ public function forUser(array $user)
+ {
+ $this->recipientUid = $user['uid'] ?? 0;
+ try {
+ $this->l10n = $user['language'] ? $this->l10n->withLang($user['language']) : $this->l10n;
+ } catch (Exception $e) {
+ $this->logger->warning('cannot use language.', ['user' => $user, 'exception' => $e]);
+ }
+
+ return $this;
+ }
+
+ /**
+ * Adds the sender to the email (if not called/set, the sender will get loaded with the help of the user id)
+ *
+ * @param string $name The name of the sender
+ * @param string $address The (email) address of the sender
+ * @param string|null $noReply Optional "no-reply" (email) address (if not set, it's the same as the address)
+ *
+ * @return static
+ */
+ public function withSender(string $name, string $address, string $noReply = null)
+ {
+ $this->senderName = $name;
+ $this->senderAddress = $address;
+ $this->senderNoReply = $noReply ?? $this->senderNoReply;
+
+ return $this;
+ }
+
+ /**
+ * Adds a recipient to the email
+ *
+ * @param string $address The (email) address of the recipient
+ *
+ * @return static
+ */
+ public function withRecipient(string $address)
+ {
+ $this->recipientAddress = $address;
+
+ return $this;
+ }
+
+ /**
+ * Build a email based on the given attributes
+ *
+ * @param bool $raw True, if the email shouldn't get extended by the default email-template
+ *
+ * @return IEmail A new generated email
+ *
+ * @throws InternalServerErrorException
+ * @throws Exception
+ */
+ public function build(bool $raw = false)
+ {
+ if ((empty($this->recipientAddress)) &&
+ !empty($this->recipientUid)) {
+ $user = User::getById($this->recipientUid, ['email']);
+
+ if (!empty($user['email'])) {
+ $this->recipientAddress = $user['email'];
+ }
+ }
+
+ if (empty($this->recipientAddress)) {
+ throw new InternalServerErrorException('Recipient address is missing.');
+ }
+
+ if (empty($this->senderAddress) || empty($this->senderName)) {
+ throw new InternalServerErrorException('Sender address or name is missing.');
+ }
+
+ $this->senderNoReply = $this->senderNoReply ?? $this->senderAddress;
+
+ $msgHtml = $this->getHtmlMessage() ?? '';
+
+ if (!$raw) {
+ // load the template for private message notifications
+ $tpl = Renderer::getMarkupTemplate('email/html.tpl');
+ $msgHtml = Renderer::replaceMacros($tpl, [
+ '$title' => $this->l10n->t('Friendica Notification'),
+ '$product' => FRIENDICA_PLATFORM,
+ '$htmlversion' => $msgHtml,
+ '$sitename' => $this->config->get('config', 'sitename'),
+ '$banner' => $this->config->get('system', 'email_banner',
+ $this->baseUrl->get(true) . DIRECTORY_SEPARATOR . self::DEFAULT_EMAIL_BANNER),
+ ]);
+ }
+
+ return new Email(
+ $this->senderName,
+ $this->senderAddress,
+ $this->senderNoReply,
+ $this->recipientAddress,
+ $this->getSubject() ?? '',
+ $msgHtml,
+ $this->getPlaintextMessage() ?? '',
+ $this->headers,
+ $this->recipientUid ?? null);
+ }
+}
diff --git a/src/Util/EMailer/SystemMailBuilder.php b/src/Util/EMailer/SystemMailBuilder.php
new file mode 100644
index 0000000000..24c1593c98
--- /dev/null
+++ b/src/Util/EMailer/SystemMailBuilder.php
@@ -0,0 +1,114 @@
+config->get('config', 'admin_name')) {
+ $this->siteAdmin = $l10n->t('%1$s, %2$s Administrator', $this->config->get('config', 'admin_name'), $siteName);
+ } else {
+ $this->siteAdmin = $l10n->t('%s Administrator', $siteName);
+ }
+
+ // Set the system wide site address/name as sender (default for system mails)
+ $this->senderName = $siteName;
+ $this->senderAddress = $siteEmailAddress;
+ $this->senderNoReply = $siteEmailAddress;
+ }
+
+ /**
+ * Adds a message
+ *
+ * @param string $subject The subject of the email
+ * @param string $preamble The preamble of the email
+ * @param string|null $body The body of the email (if not set, the preamble will get used as body)
+ *
+ * @return static
+ */
+ public function withMessage(string $subject, string $preamble, string $body = null)
+ {
+ if (!isset($body)) {
+ $body = $preamble;
+ }
+
+ $this->subject = $subject;
+ $this->preamble = $preamble;
+ $this->body = $body;
+
+ return $this;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected function getSubject()
+ {
+ return $this->subject;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws InternalServerErrorException
+ * @throws Exception
+ */
+ protected function getHtmlMessage()
+ {
+ $htmlVersion = BBCode::convert($this->body);
+
+ // load the template for private message notifications
+ $tpl = Renderer::getMarkupTemplate('email/system/html.tpl');
+ return Renderer::replaceMacros($tpl, [
+ '$preamble' => str_replace("\n", "
\n", $this->preamble),
+ '$thanks' => $this->l10n->t('thanks'),
+ '$site_admin' => $this->siteAdmin,
+ '$htmlversion' => $htmlVersion,
+ ]);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws Exception
+ */
+ protected function getPlaintextMessage()
+ {
+ $textVersion = BBCode::toPlaintext($this->body);
+
+ // load the template for private message notifications
+ $tpl = Renderer::getMarkupTemplate('email/system/text.tpl');
+ return Renderer::replaceMacros($tpl, [
+ '$preamble' => $this->preamble,
+ '$thanks' => $this->l10n->t('thanks'),
+ '$site_admin' => $this->siteAdmin,
+ '$textversion' => $textVersion,
+ ]);
+ }
+}
diff --git a/src/Util/Emailer.php b/src/Util/Emailer.php
index 19755bebda..1e1c6856cf 100644
--- a/src/Util/Emailer.php
+++ b/src/Util/Emailer.php
@@ -7,10 +7,12 @@ namespace Friendica\Util;
use Friendica\App;
use Friendica\Core\Config\IConfig;
use Friendica\Core\Hook;
+use Friendica\Core\L10n;
use Friendica\Core\PConfig\IPConfig;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Object\EMail\IEmail;
use Friendica\Protocol\Email;
+use Friendica\Util\EMailer\SystemMailBuilder;
use Psr\Log\LoggerInterface;
/**
@@ -26,13 +28,65 @@ class Emailer
private $logger;
/** @var App\BaseURL */
private $baseUrl;
+ /** @var L10n */
+ private $l10n;
- public function __construct(IConfig $config, IPConfig $pConfig, App\BaseURL $baseURL, LoggerInterface $logger)
+ /** @var string */
+ private $siteEmailAddress;
+ /** @var string */
+ private $siteEmailName;
+
+ public function __construct(IConfig $config, IPConfig $pConfig, App\BaseURL $baseURL, LoggerInterface $logger,
+ L10n $defaultLang)
{
$this->config = $config;
$this->pConfig = $pConfig;
$this->logger = $logger;
$this->baseUrl = $baseURL;
+ $this->l10n = $defaultLang;
+
+ $this->siteEmailAddress = $this->config->get('config', 'sender_email');
+ if (empty($sysEmailAddress)) {
+ $hostname = $this->baseUrl->getHostname();
+ if (strpos($hostname, ':')) {
+ $hostname = substr($hostname, 0, strpos($hostname, ':'));
+ }
+
+ $this->siteEmailAddress = 'noreply@' . $hostname;
+ }
+
+ $this->siteEmailName = $this->config->get('config', 'sitename', 'Friendica Social Network');
+ }
+
+ /**
+ * Gets the site's default sender email address
+ *
+ * @return string
+ */
+ public function getSiteEmailAddress()
+ {
+ return $this->siteEmailAddress;
+ }
+
+ /**
+ * Gets the site's default sender name
+ *
+ * @return string
+ */
+ public function getSiteEmailName()
+ {
+ return $this->siteEmailName;
+ }
+
+ /**
+ * Creates a new system email
+ *
+ * @return SystemMailBuilder
+ */
+ public function newSystemMail()
+ {
+ return new SystemMailBuilder($this->l10n, $this->baseUrl, $this->config, $this->logger,
+ $this->getSiteEmailAddress(), $this->getSiteEmailName());
}
/**
diff --git a/tests/Util/SampleMailBuilder.php b/tests/Util/SampleMailBuilder.php
new file mode 100644
index 0000000000..59638eea97
--- /dev/null
+++ b/tests/Util/SampleMailBuilder.php
@@ -0,0 +1,57 @@
+subject = $subject;
+ $this->html = $html;
+ $this->text = $text;
+
+ return $this;
+ }
+
+ /**
+ * @inheritDoc
+ */
+ protected function getSubject()
+ {
+ return $this->subject;
+ }
+
+ /**
+ * @inheritDoc
+ */
+ protected function getHtmlMessage()
+ {
+ return $this->html;
+ }
+
+ /**
+ * @inheritDoc
+ */
+ protected function getPlaintextMessage()
+ {
+ return $this->text;
+ }
+}
diff --git a/tests/src/Util/Emailer/MailBuilderTest.php b/tests/src/Util/Emailer/MailBuilderTest.php
new file mode 100644
index 0000000000..1e475c9f70
--- /dev/null
+++ b/tests/src/Util/Emailer/MailBuilderTest.php
@@ -0,0 +1,177 @@
+setUpVfsDir();
+
+ $this->config = \Mockery::mock(IConfig::class);
+ $this->l10n = \Mockery::mock(L10n::class);
+ $this->baseUrl = \Mockery::mock(BaseURL::class);
+ $this->baseUrl->shouldReceive('getHostname')->andReturn('friendica.local');
+ $this->baseUrl->shouldReceive('get')->andReturn('http://friendica.local');
+
+ $this->defaultHeaders = "";
+ }
+
+ public function assertEmail(IEmail $email, array $asserts)
+ {
+ $this->assertEquals($asserts['subject'] ?? $email->getSubject(), $email->getSubject());
+ $this->assertEquals($asserts['html'] ?? $email->getMessage(), $email->getMessage());
+ $this->assertEquals($asserts['text'] ?? $email->getMessage(true), $email->getMessage(true));
+ $this->assertEquals($asserts['toAddress'] ?? $email->getToAddress(), $email->getToAddress());
+ $this->assertEquals($asserts['fromAddress'] ?? $email->getFromAddress(), $email->getFromAddress());
+ $this->assertEquals($asserts['fromName'] ?? $email->getFromName(), $email->getFromName());
+ $this->assertEquals($asserts['replyTo'] ?? $email->getReplyTo(), $email->getReplyTo());
+ $this->assertEquals($asserts['uid'] ?? $email->getRecipientUid(), $email->getRecipientUid());
+ $this->assertEquals($asserts['header'] ?? $email->getAdditionalMailHeader(), $email->getAdditionalMailHeader());
+ }
+
+ /**
+ * Test if the builder instance can get created
+ */
+ public function testBuilderInstance()
+ {
+ $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
+
+ $this->assertInstanceOf(MailBuilder::class, $builder);
+ }
+
+ /**
+ * Test if the builder can create full rendered emails
+ *
+ * @todo Create test once "Renderer" and "BBCode" are dynamic
+ */
+ public function testBuilderWithNonRawEmail()
+ {
+ $this->markTestIncomplete('Cannot easily mock Renderer and BBCode, so skipping tests wit them');
+ }
+
+ /**
+ * Test if the builder can create a "simple" raw mail
+ */
+ public function testBuilderWithRawEmail()
+ {
+ $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
+
+ $testEmail = $builder
+ ->withMessage('Subject', 'Html', 'text')
+ ->withRecipient('recipient@friendica.local')
+ ->withSender('Sender', 'sender@friendica.local', 'no-reply@friendica.local')
+ ->forUser(['uid' => 100])
+ ->build(true);
+
+ $this->assertEmail($testEmail, [
+ 'subject' => 'Subject',
+ 'html' => 'Html',
+ 'text' => 'text',
+ 'toAddress' => 'recipient@friendica.local',
+ 'fromName' => 'Sender',
+ 'fromAddress' => 'sender@friendica.local',
+ 'noReply' => 'no-reply@friendica.local',
+ 'uid' => 100,
+ 'headers' => $this->defaultHeaders,
+ ]);
+ }
+
+ /**
+ * Test if the builder throws an exception in case no recipient
+ *
+ * @expectedException \Friendica\Network\HTTPException\InternalServerErrorException
+ * @expectedExceptionMessage Recipient address is missing.
+ */
+ public function testBuilderWithEmptyMail()
+ {
+ $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
+
+ $builder->build(true);
+ }
+
+ /**
+ * Test if the builder throws an exception in case no sender
+ *
+ * @expectedException \Friendica\Network\HTTPException\InternalServerErrorException
+ * @expectedExceptionMessage Sender address or name is missing.
+ */
+ public function testBuilderWithEmptySender()
+ {
+ $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
+
+ $builder
+ ->withRecipient('test@friendica.local')
+ ->build(true);
+ }
+
+ /**
+ * Test if the builder is capable of creating "empty" mails if needed (not the decision of the builder if so ..)
+ */
+ public function testBuilderWithoutMessage()
+ {
+ $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
+
+ $testEmail = $builder
+ ->withRecipient('recipient@friendica.local')
+ ->withSender('Sender', 'sender@friendica.local')
+ ->build(true);
+
+ $this->assertEmail($testEmail, [
+ 'toAddress' => 'recipient@friendica.local',
+ 'fromName' => 'Sender',
+ 'fromAddress' => 'sender@friendica.local',
+ 'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set
+ 'headers' => $this->defaultHeaders,
+ ]);
+ }
+
+ /**
+ * Test if the builder sets for the text the same as for
+ */
+ public function testBuilderWithJustPreamble()
+ {
+ $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
+
+ $testEmail = $builder
+ ->withRecipient('recipient@friendica.local')
+ ->withSender('Sender', 'sender@friendica.local')
+ ->build(true);
+
+ $this->assertEmail($testEmail, [
+ 'toAddress' => 'recipient@friendica.local',
+ 'fromName' => 'Sender',
+ 'fromAddress' => 'sender@friendica.local',
+ 'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set,
+ 'headers' => $this->defaultHeaders,
+ ]);
+ }
+}
diff --git a/tests/src/Util/Emailer/SystemMailBuilderTest.php b/tests/src/Util/Emailer/SystemMailBuilderTest.php
new file mode 100644
index 0000000000..e855602766
--- /dev/null
+++ b/tests/src/Util/Emailer/SystemMailBuilderTest.php
@@ -0,0 +1,57 @@
+setUpVfsDir();
+
+ $this->config = \Mockery::mock(IConfig::class);
+ $this->config->shouldReceive('get')->with('config', 'admin_name')->andReturn('Admin');
+ $this->l10n = \Mockery::mock(L10n::class);
+ $this->l10n->shouldReceive('t')->andReturnUsing(function ($msg) {
+ return $msg;
+ });
+ $this->baseUrl = \Mockery::mock(BaseURL::class);
+ $this->baseUrl->shouldReceive('getHostname')->andReturn('friendica.local');
+ $this->baseUrl->shouldReceive('get')->andReturn('http://friendica.local');
+
+ $this->defaultHeaders = "";
+ }
+
+ /**
+ * Test if the builder instance can get created
+ */
+ public function testBuilderInstance()
+ {
+ $builder = new SystemMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger(), 'moreply@friendica.local', 'FriendicaSite');
+
+ $this->assertInstanceOf(MailBuilder::class, $builder);
+ $this->assertInstanceOf(SystemMailBuilder::class, $builder);
+ }
+}
diff --git a/view/templates/admin/site.tpl b/view/templates/admin/site.tpl
index 19774b4255..9ccda33eeb 100644
--- a/view/templates/admin/site.tpl
+++ b/view/templates/admin/site.tpl
@@ -15,6 +15,7 @@
{{include file="field_input.tpl" field=$sitename}}
{{include file="field_input.tpl" field=$sender_email}}
{{include file="field_textarea.tpl" field=$banner}}
+ {{include file="field_input.tpl" field=$email_banner}}
{{include file="field_input.tpl" field=$shortcut_icon}}
{{include file="field_input.tpl" field=$touch_icon}}
{{include file="field_textarea.tpl" field=$additional_info}}
diff --git a/view/templates/email/html.tpl b/view/templates/email/html.tpl
new file mode 100644
index 0000000000..103572eaed
--- /dev/null
+++ b/view/templates/email/html.tpl
@@ -0,0 +1,23 @@
+
+
+
+ {{$title}}
+
+
+
+
+
+
+
+
+ {{$product}}
+
+ |
+
+
+
+
+ {{$htmlversion nofilter}}
+
+
+
diff --git a/view/templates/email_notify_html.tpl b/view/templates/email/notify/html.tpl
similarity index 100%
rename from view/templates/email_notify_html.tpl
rename to view/templates/email/notify/html.tpl
diff --git a/view/templates/email_notify_text.tpl b/view/templates/email/notify/text.tpl
similarity index 100%
rename from view/templates/email_notify_text.tpl
rename to view/templates/email/notify/text.tpl
diff --git a/view/templates/email/system/html.tpl b/view/templates/email/system/html.tpl
new file mode 100644
index 0000000000..c34ddaba85
--- /dev/null
+++ b/view/templates/email/system/html.tpl
@@ -0,0 +1,5 @@
+
+{{$htmlversion nofilter}} |
+{{$thanks}} |
+{{$site_admin}} |
+
\ No newline at end of file
diff --git a/view/templates/email/system/text.tpl b/view/templates/email/system/text.tpl
new file mode 100644
index 0000000000..4dd7c8ccbd
--- /dev/null
+++ b/view/templates/email/system/text.tpl
@@ -0,0 +1,7 @@
+
+{{$preamble nofilter}}
+
+{{$textversion nofilter}}
+
+{{$thanks nofilter}}
+{{$site_admin nofilter}}