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