3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Util\EMailer;
25 use Friendica\App\BaseURL;
26 use Friendica\Content\Text\BBCode;
27 use Friendica\Core\Config\Capability\IManageConfigValues;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Renderer;
30 use Friendica\Network\HTTPException\InternalServerErrorException;
31 use Psr\Log\LoggerInterface;
34 * Builder for notification emails (notification, source, links, ...)
36 class NotifyMailBuilder extends MailBuilder
49 private $contentAllowed = false;
52 /** @var array Details to print a photo:
62 /** @var array HTML/Plain version of the Site Link:
70 /** @var string The item link */
71 private $itemLink = '';
73 public function __construct(L10n $l10n, BaseURL $baseUrl, IManageConfigValues $config, LoggerInterface $logger, string $siteEmailAddress, string $siteName)
75 parent::__construct($l10n, $baseUrl, $config, $logger);
77 if ($this->config->get('config', 'admin_name')) {
78 $this->siteAdmin = $l10n->t('%1$s, %2$s Administrator', $this->config->get('config', 'admin_name'), $siteName);
80 $this->siteAdmin = $l10n->t('%s Administrator', $siteName);
83 // Set the system wide site address/name as sender (default for system mails)
84 $this->withSender($siteName, $siteEmailAddress, $siteEmailAddress);
86 // check whether sending post content in email notifications is allowed
87 $this->contentAllowed = !$this->config->get('system', 'enotify_no_content', false);
91 * Adds a notification (in fact a more detailed message)
93 * @param string $subject
94 * @param string $preamble
95 * @param string $title
96 * @param string|null $body
100 public function withNotification(string $subject, string $preamble, string $title, string $body = null)
106 $this->title = stripslashes($title);
107 $this->subject = $subject;
108 $this->preamble = $preamble;
115 * Adds a photo of the source of the notify
117 * @param string $image The image link to the photo
118 * @param string $link The link to the source
119 * @param string $name The name of the source
123 public function withPhoto(string $image, string $link, string $name)
126 'image' => $image ?? '',
127 'link' => $link ?? '',
128 'name' => $name ?? '',
135 * Adds a sitelink to the notification
137 * @param string $text The text version of the site link
138 * @param string $html The html version of the site link
142 public function withSiteLink(string $text, string $html = '')
153 * Adds a link to the item of the notification
155 * @param string $link The text version of the item link
159 public function withItemLink(string $link)
161 $this->itemLink = $link;
169 protected function getSubject()
171 return $this->subject;
177 * @throws InternalServerErrorException
180 protected function getHtmlMessage()
182 $htmlVersion = BBCode::convert($this->body);
184 // load the template for private message notifications
185 $tpl = Renderer::getMarkupTemplate('email/notify/html.tpl');
186 return Renderer::replaceMacros($tpl, [
187 '$preamble' => str_replace("\n", "<br>\n", $this->preamble),
188 '$source_name' => $this->photo['name'],
189 '$source_link' => $this->photo['link'],
190 '$source_photo' => $this->photo['image'],
191 '$hsitelink' => $this->siteLink['html'],
192 '$hitemlink' => sprintf('<a href="%s">%s</a>', $this->itemLink, $this->itemLink),
193 '$thanks' => $this->l10n->t('thanks'),
194 '$site_admin' => $this->siteAdmin,
195 '$title' => $this->title,
196 '$htmlversion' => $htmlVersion,
197 '$content_allowed' => $this->contentAllowed,
207 protected function getPlaintextMessage()
209 $textVersion = BBCode::toPlaintext($this->body);
211 // load the template for private message notifications
212 $tpl = Renderer::getMarkupTemplate('email/notify/text.tpl');
213 return Renderer::replaceMacros($tpl, [
214 '$preamble' => $this->preamble,
215 '$tsitelink' => $this->siteLink['text'],
216 '$titemlink' => $this->itemLink,
217 '$thanks' => $this->l10n->t('thanks'),
218 '$site_admin' => $this->siteAdmin,
219 '$title' => $this->title,
220 '$textversion' => $textVersion,
221 '$content_allowed' => $this->contentAllowed,