3 namespace Friendica\Util\EMailer;
6 use Friendica\App\BaseURL;
7 use Friendica\Core\Config\IConfig;
8 use Friendica\Core\L10n;
9 use Friendica\Core\Renderer;
10 use Friendica\Model\User;
11 use Friendica\Network\HTTPException\InternalServerErrorException;
12 use Friendica\Object\Email;
13 use Friendica\Object\EMail\IEmail;
14 use Psr\Log\LoggerInterface;
17 * A base class for building new emails
19 abstract class MailBuilder
21 /** @var string The default email banner in case nothing else is defined */
22 const DEFAULT_EMAIL_BANNER = 'images/friendica-32.png';
30 /** @var LoggerInterface */
37 protected $senderName = null;
39 protected $senderAddress = null;
41 protected $senderNoReply = null;
44 protected $recipientAddress = null;
46 protected $recipientUid = null;
48 public function __construct(L10n $l10n, BaseURL $baseUrl, IConfig $config, LoggerInterface $logger)
51 $this->baseUrl = $baseUrl;
52 $this->config = $config;
53 $this->logger = $logger;
55 $hostname = $baseUrl->getHostname();
56 if (strpos($hostname, ':')) {
57 $hostname = substr($hostname, 0, strpos($hostname, ':'));
61 $this->headers .= "Precedence: list\n";
62 $this->headers .= "X-Friendica-Host: " . $hostname . "\n";
63 $this->headers .= "X-Friendica-Platform: " . FRIENDICA_PLATFORM . "\n";
64 $this->headers .= "X-Friendica-Version: " . FRIENDICA_VERSION . "\n";
65 $this->headers .= "List-ID: <notification." . $hostname . ">\n";
66 $this->headers .= "List-Archive: <" . $baseUrl->get() . "/notifications/system>\n";
70 * Gets the subject of the concrete builder, which inherits this base class
74 abstract protected function getSubject();
77 * Gets the HTML version of the body of the concrete builder, which inherits this base class
81 abstract protected function getHtmlMessage();
84 * Gets the Plaintext version of the body of the concrete builder, which inherits this base class
88 abstract protected function getPlaintextMessage();
91 * Adds the User ID to the email in case the mail sending needs additional properties of this user
93 * @param array $user The user entity/array, for which the email should be sent
96 * @todo Once the user array is replaced with a user entity, replace this array parameter as well
98 public function forUser(array $user)
100 $this->recipientUid = $user['uid'] ?? 0;
102 $this->l10n = $user['language'] ? $this->l10n->withLang($user['language']) : $this->l10n;
103 } catch (Exception $e) {
104 $this->logger->warning('cannot use language.', ['user' => $user, 'exception' => $e]);
111 * Adds the sender to the email (if not called/set, the sender will get loaded with the help of the user id)
113 * @param string $name The name of the sender
114 * @param string $address The (email) address of the sender
115 * @param string|null $noReply Optional "no-reply" (email) address (if not set, it's the same as the address)
119 public function withSender(string $name, string $address, string $noReply = null)
121 $this->senderName = $name;
122 $this->senderAddress = $address;
123 $this->senderNoReply = $noReply ?? $this->senderNoReply;
129 * Adds a recipient to the email
131 * @param string $address The (email) address of the recipient
135 public function withRecipient(string $address)
137 $this->recipientAddress = $address;
143 * Build a email based on the given attributes
145 * @param bool $raw True, if the email shouldn't get extended by the default email-template
147 * @return IEmail A new generated email
149 * @throws InternalServerErrorException
152 public function build(bool $raw = false)
154 if ((empty($this->recipientAddress)) &&
155 !empty($this->recipientUid)) {
156 $user = User::getById($this->recipientUid, ['email']);
158 if (!empty($user['email'])) {
159 $this->recipientAddress = $user['email'];
163 if (empty($this->recipientAddress)) {
164 throw new InternalServerErrorException('Recipient address is missing.');
167 if (empty($this->senderAddress) || empty($this->senderName)) {
168 throw new InternalServerErrorException('Sender address or name is missing.');
171 $this->senderNoReply = $this->senderNoReply ?? $this->senderAddress;
173 $msgHtml = $this->getHtmlMessage() ?? '';
176 // load the template for private message notifications
177 $tpl = Renderer::getMarkupTemplate('email/html.tpl');
178 $msgHtml = Renderer::replaceMacros($tpl, [
179 '$title' => $this->l10n->t('Friendica Notification'),
180 '$product' => FRIENDICA_PLATFORM,
181 '$htmlversion' => $msgHtml,
182 '$sitename' => $this->config->get('config', 'sitename'),
183 '$banner' => $this->config->get('system', 'email_banner',
184 $this->baseUrl->get(true) . DIRECTORY_SEPARATOR . self::DEFAULT_EMAIL_BANNER),
190 $this->senderAddress,
191 $this->senderNoReply,
192 $this->recipientAddress,
193 $this->getSubject() ?? '',
195 $this->getPlaintextMessage() ?? '',
197 $this->recipientUid ?? null);