]> git.mxchange.org Git - friendica.git/blob - src/Util/EMailer/SystemMailBuilder.php
Add license info at Friendica classes
[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|null $body     The body of the email (if not set, the preamble will get used as body)
69          *
70          * @return static
71          */
72         public function withMessage(string $subject, string $preamble, string $body = null)
73         {
74                 if (!isset($body)) {
75                         $body = $preamble;
76                 }
77
78                 $this->subject  = $subject;
79                 $this->preamble = $preamble;
80                 $this->body     = $body;
81
82                 return $this;
83         }
84
85         /**
86          * {@inheritDoc}
87          */
88         protected function getSubject()
89         {
90                 return $this->subject;
91         }
92
93         /**
94          * {@inheritDoc}
95          *
96          * @throws InternalServerErrorException
97          * @throws Exception
98          */
99         protected function getHtmlMessage()
100         {
101                 $htmlVersion = BBCode::convert($this->body);
102
103                 // load the template for private message notifications
104                 $tpl = Renderer::getMarkupTemplate('email/system/html.tpl');
105                 return Renderer::replaceMacros($tpl, [
106                         '$preamble'    => str_replace("\n", "<br>\n", $this->preamble),
107                         '$thanks'      => $this->l10n->t('thanks'),
108                         '$site_admin'  => $this->siteAdmin,
109                         '$htmlversion' => $htmlVersion,
110                 ]);
111         }
112
113         /**
114          * {@inheritDoc}
115          *
116          * @throws Exception
117          */
118         protected function getPlaintextMessage()
119         {
120                 $textVersion = BBCode::toPlaintext($this->body);
121
122                 // load the template for private message notifications
123                 $tpl = Renderer::getMarkupTemplate('email/system/text.tpl');
124                 return Renderer::replaceMacros($tpl, [
125                         '$preamble'    => $this->preamble,
126                         '$thanks'      => $this->l10n->t('thanks'),
127                         '$site_admin'  => $this->siteAdmin,
128                         '$textversion' => $textVersion,
129                 ]);
130         }
131 }