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