]> git.mxchange.org Git - friendica.git/blob - src/Util/EMailer/SystemMailBuilder.php
Set sender data directly
[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->senderName    = $siteName;
41                 $this->senderAddress = $siteEmailAddress;
42                 $this->senderNoReply = $siteEmailAddress;
43         }
44
45         /**
46          * Adds a message
47          *
48          * @param string      $subject  The subject of the email
49          * @param string      $preamble The preamble of the email
50          * @param string|null $body     The body of the email (if not set, the preamble will get used as body)
51          *
52          * @return static
53          */
54         public function withMessage(string $subject, string $preamble, string $body = null)
55         {
56                 if (!isset($body)) {
57                         $body = $preamble;
58                 }
59
60                 $this->subject  = $subject;
61                 $this->preamble = $preamble;
62                 $this->body     = $body;
63
64                 return $this;
65         }
66
67         /**
68          * {@inheritDoc}
69          */
70         protected function getSubject()
71         {
72                 return $this->subject;
73         }
74
75         /**
76          * {@inheritDoc}
77          *
78          * @throws InternalServerErrorException
79          * @throws Exception
80          */
81         protected function getHtmlMessage()
82         {
83                 $htmlVersion = BBCode::convert($this->body);
84
85                 // load the template for private message notifications
86                 $tpl = Renderer::getMarkupTemplate('email/system/html.tpl');
87                 return Renderer::replaceMacros($tpl, [
88                         '$preamble'    => str_replace("\n", "<br>\n", $this->preamble),
89                         '$thanks'      => $this->l10n->t('thanks'),
90                         '$site_admin'  => $this->siteAdmin,
91                         '$htmlversion' => $htmlVersion,
92                 ]);
93         }
94
95         /**
96          * {@inheritDoc}
97          *
98          * @throws Exception
99          */
100         protected function getPlaintextMessage()
101         {
102                 $textVersion = BBCode::toPlaintext($this->body);
103
104                 // load the template for private message notifications
105                 $tpl = Renderer::getMarkupTemplate('email/system/text.tpl');
106                 return Renderer::replaceMacros($tpl, [
107                         '$preamble'    => $this->preamble,
108                         '$thanks'      => $this->l10n->t('thanks'),
109                         '$site_admin'  => $this->siteAdmin,
110                         '$textversion' => $textVersion,
111                 ]);
112         }
113 }