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;
16 * A base class for building new emails
18 abstract class MailBuilder
20 /** @var string The default email banner in case nothing else is defined */
21 const DEFAULT_EMAIL_BANNER = 'images/friendica-32.png';
34 protected $senderName = null;
36 protected $senderAddress = null;
38 protected $senderNoReply = null;
41 protected $recipientAddress = null;
43 protected $recipientUid = null;
45 public function __construct(L10n $l10n, BaseURL $baseUrl, IConfig $config)
48 $this->baseUrl = $baseUrl;
49 $this->config = $config;
51 $hostname = $baseUrl->getHostname();
52 if (strpos($hostname, ':')) {
53 $hostname = substr($hostname, 0, strpos($hostname, ':'));
57 $this->headers .= "Precedence: list\n";
58 $this->headers .= "X-Friendica-Host: " . $hostname . "\n";
59 $this->headers .= "X-Friendica-Platform: " . FRIENDICA_PLATFORM . "\n";
60 $this->headers .= "X-Friendica-Version: " . FRIENDICA_VERSION . "\n";
61 $this->headers .= "List-ID: <notification." . $hostname . ">\n";
62 $this->headers .= "List-Archive: <" . $baseUrl->get() . "/notifications/system>\n";
66 * Gets the subject of the concrete builder, which inherits this base class
70 abstract protected function getSubject();
73 * Gets the HTML version of the body of the concrete builder, which inherits this base class
77 abstract protected function getHtmlMessage();
80 * Gets the Plaintext version of the body of the concrete builder, which inherits this base class
84 abstract protected function getPlaintextMessage();
87 * Adds the User ID to the email in case the mail sending needs additional properties of this user
89 * @param int $uid The User ID
93 public function forUser(int $uid)
95 $this->recipientUid = $uid;
101 * Adds the sender to the email (if not called/set, the sender will get loaded with the help of the user id)
103 * @param string $name The name of the sender
104 * @param string $address The (email) address of the sender
105 * @param string|null $noReply Optional "no-reply" (email) address (if not set, it's the same as the address)
109 public function withSender(string $name, string $address, string $noReply = null)
111 $this->senderName = $name;
112 $this->senderAddress = $address;
113 $this->senderNoReply = $noReply ?? $this->senderNoReply;
119 * Adds a recipient to the email
121 * @param string $address The (email) address of the recipient
125 public function withRecipient(string $address)
127 $this->recipientAddress = $address;
133 * Build a email based on the given attributes
135 * @param bool $raw True, if the email shouldn't get extended by the default email-template
137 * @return IEmail A new generated email
139 * @throws InternalServerErrorException
142 public function build(bool $raw = false)
144 if ((empty($this->recipientAddress)) &&
145 !empty($this->recipientUid)) {
146 $user = User::getById($this->recipientUid, ['email']);
148 if (!empty($user['email'])) {
149 $this->recipientAddress = $user['email'];
153 if (empty($this->recipientAddress)) {
154 throw new InternalServerErrorException('Recipient address is missing.');
157 if (empty($this->senderAddress) || empty($this->senderName)) {
158 throw new InternalServerErrorException('Sender address or name is missing.');
161 $this->senderNoReply = $this->senderNoReply ?? $this->senderAddress;
163 $msgHtml = $this->getHtmlMessage() ?? '';
166 // load the template for private message notifications
167 $tpl = Renderer::getMarkupTemplate('email/html.tpl');
168 $msgHtml = Renderer::replaceMacros($tpl, [
169 '$title' => $this->l10n->t('Friendica Notification'),
170 '$product' => FRIENDICA_PLATFORM,
171 '$htmlversion' => $msgHtml,
172 '$sitename' => $this->config->get('config', 'sitename'),
173 '$banner' => $this->config->get('system', 'email_banner',
174 $this->baseUrl->get(true) . DIRECTORY_SEPARATOR . self::DEFAULT_EMAIL_BANNER),
180 $this->senderAddress,
181 $this->senderNoReply,
182 $this->recipientAddress,
183 $this->getSubject() ?? '',
185 $this->getPlaintextMessage() ?? '',
187 $this->recipientUid ?? null);