]> git.mxchange.org Git - friendica.git/blob - src/Util/EMailer/SystemMailBuilder.php
93e5cc349f4664c348fffcdffa62631f1f208b73
[friendica.git] / src / Util / EMailer / SystemMailBuilder.php
1 <?php
2
3 namespace Friendica\Util\EMailer;
4
5 use Exception;
6 use Friendica\App;
7 use Friendica\App\BaseURL;
8 use Friendica\Content\Text\BBCode;
9 use Friendica\Core\Config\IConfig;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Renderer;
12 use Friendica\Network\HTTPException\InternalServerErrorException;
13
14 /**
15  * Builder for system-wide emails without any dependency to concrete entities (like items, activities, ..)
16  */
17 class SystemMailBuilder extends MailBuilder
18 {
19         /** @var string */
20         protected $subject;
21         /** @var string */
22         protected $preamble;
23         /** @var string */
24         protected $body;
25
26         /** @var string */
27         protected $siteAdmin;
28
29         public function __construct(L10n $l10n, BaseURL $baseUrl, IConfig $config, string $siteEmailAddress, string $siteName)
30         {
31                 parent::__construct($l10n, $baseUrl, $config);
32
33                 if ($this->config->get('config', 'admin_name')) {
34                         $this->siteAdmin = $l10n->t('%1$s, %2$s Administrator', $this->config->get('config', 'admin_name'), $siteName);
35                 } else {
36                         $this->siteAdmin = $l10n->t('%s Administrator', $siteName);
37                 }
38
39                 // Set the system wide site address/name as sender (default for system mails)
40                 $this->withSender($siteEmailAddress, $siteName);
41         }
42
43         /**
44          * Adds a message
45          *
46          * @param string      $subject  The subject of the email
47          * @param string      $preamble The preamble of the email
48          * @param string|null $body     The body of the email (if not set, the preamble will get used as body)
49          *
50          * @return static
51          */
52         public function withMessage(string $subject, string $preamble, string $body = null)
53         {
54                 if (!isset($body)) {
55                         $body = $preamble;
56                 }
57
58                 $this->subject  = $subject;
59                 $this->preamble = $preamble;
60                 $this->body     = $body;
61
62                 return $this;
63         }
64
65         /**
66          * {@inheritDoc}
67          */
68         protected function getSubject()
69         {
70                 return $this->subject;
71         }
72
73         /**
74          * {@inheritDoc}
75          *
76          * @throws InternalServerErrorException
77          * @throws Exception
78          */
79         protected function getHtmlMessage()
80         {
81                 $htmlVersion = BBCode::convert($this->body);
82
83                 // load the template for private message notifications
84                 $tpl = Renderer::getMarkupTemplate('email/system/html.tpl');
85                 return Renderer::replaceMacros($tpl, [
86                         '$preamble'    => str_replace("\n", "<br>\n", $this->preamble),
87                         '$thanks'      => $this->l10n->t('thanks'),
88                         '$site_admin'  => $this->siteAdmin,
89                         '$htmlversion' => $htmlVersion,
90                 ]);
91         }
92
93         /**
94          * {@inheritDoc}
95          *
96          * @throws Exception
97          */
98         protected function getPlaintextMessage()
99         {
100                 $textVersion = BBCode::toPlaintext($this->body);
101
102                 // load the template for private message notifications
103                 $tpl = Renderer::getMarkupTemplate('email/system/text.tpl');
104                 return Renderer::replaceMacros($tpl, [
105                         '$preamble'    => $this->preamble,
106                         '$thanks'      => $this->l10n->t('thanks'),
107                         '$site_admin'  => $this->siteAdmin,
108                         '$textversion' => $textVersion,
109                 ]);
110         }
111 }