]> git.mxchange.org Git - friendica.git/blob - src/Util/EMailer/SystemMailBuilder.php
Optimize "withMessage()" default
[friendica.git] / src / Util / EMailer / SystemMailBuilder.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Util\EMailer;
23
24 use Exception;
25 use Friendica\App\BaseURL;
26 use Friendica\Content\Text\BBCode;
27 use Friendica\Core\Config\IConfig;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Renderer;
30 use Friendica\Network\HTTPException\InternalServerErrorException;
31 use Psr\Log\LoggerInterface;
32
33 /**
34  * Builder for system-wide emails without any dependency to concrete entities (like items, activities, ..)
35  */
36 class SystemMailBuilder extends MailBuilder
37 {
38         /** @var string */
39         protected $subject = '';
40         /** @var string */
41         protected $preamble = '';
42         /** @var string */
43         protected $body = '';
44
45         /** @var string */
46         protected $siteAdmin;
47
48         public function __construct(L10n $l10n, BaseURL $baseUrl, IConfig $config, LoggerInterface $logger,
49                                     string $siteEmailAddress, string $siteName)
50         {
51                 parent::__construct($l10n, $baseUrl, $config, $logger);
52
53                 if ($this->config->get('config', 'admin_name')) {
54                         $this->siteAdmin = $l10n->t('%1$s, %2$s Administrator', $this->config->get('config', 'admin_name'), $siteName);
55                 } else {
56                         $this->siteAdmin = $l10n->t('%s Administrator', $siteName);
57                 }
58
59                 // Set the system wide site address/name as sender (default for system mails)
60                 $this->withSender($siteName, $siteEmailAddress, $siteEmailAddress);
61         }
62
63         /**
64          * Adds a message
65          *
66          * @param string $subject  The subject of the email
67          * @param string $preamble The preamble of the email
68          * @param string $body     The body of the email (optional)
69          *
70          * @return static
71          */
72         public function withMessage(string $subject, string $preamble, string $body = '')
73         {
74                 $this->subject  = $subject;
75                 $this->preamble = $preamble;
76                 $this->body     = $body;
77
78                 return $this;
79         }
80
81         /**
82          * {@inheritDoc}
83          */
84         protected function getSubject()
85         {
86                 return $this->subject;
87         }
88
89         /**
90          * {@inheritDoc}
91          *
92          * @throws InternalServerErrorException
93          * @throws Exception
94          */
95         protected function getHtmlMessage()
96         {
97                 // load the template for private message notifications
98                 $tpl = Renderer::getMarkupTemplate('email/system/html.tpl');
99                 return Renderer::replaceMacros($tpl, [
100                         '$preamble'    => str_replace("\n", "<br>\n", $this->preamble),
101                         '$thanks'      => $this->l10n->t('thanks'),
102                         '$site_admin'  => $this->siteAdmin,
103                         '$htmlversion' => BBCode::convert($this->body),
104                 ]);
105         }
106
107         /**
108          * {@inheritDoc}
109          *
110          * @throws Exception
111          */
112         protected function getPlaintextMessage()
113         {
114                 // load the template for private message notifications
115                 $tpl = Renderer::getMarkupTemplate('email/system/text.tpl');
116                 return Renderer::replaceMacros($tpl, [
117                         '$preamble'    => $this->preamble,
118                         '$thanks'      => $this->l10n->t('thanks'),
119                         '$site_admin'  => $this->siteAdmin,
120                         '$textversion' => BBCode::toPlaintext($this->body),
121                 ]);
122         }
123 }