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;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Friendica\Core\Hook;
27 use Friendica\Core\L10n;
28 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
29 use Friendica\Network\HTTPException\InternalServerErrorException;
30 use Friendica\Object\EMail\IEmail;
31 use Friendica\Protocol\Email;
32 use Friendica\Util\EMailer\NotifyMailBuilder;
33 use Friendica\Util\EMailer\SystemMailBuilder;
34 use Psr\Log\LoggerInterface;
37 * class to handle emailing
41 /** @var IManageConfigValues */
43 /** @var IManagePersonalConfigValues */
45 /** @var LoggerInterface */
47 /** @var App\BaseURL */
53 private $siteEmailAddress;
55 private $siteEmailName;
57 public function __construct(IManageConfigValues $config, IManagePersonalConfigValues $pConfig, App\BaseURL $baseURL, LoggerInterface $logger,
60 $this->config = $config;
61 $this->pConfig = $pConfig;
62 $this->logger = $logger;
63 $this->baseUrl = $baseURL;
64 $this->l10n = $defaultLang;
66 $this->siteEmailAddress = $this->config->get('config', 'sender_email');
67 if (empty($this->siteEmailAddress)) {
68 $hostname = $this->baseUrl->getHostname();
69 if (strpos($hostname, ':')) {
70 $hostname = substr($hostname, 0, strpos($hostname, ':'));
73 $this->siteEmailAddress = 'noreply@' . $hostname;
76 $this->siteEmailName = $this->config->get('config', 'sitename', 'Friendica Social Network');
80 * Gets the site's default sender email address
84 public function getSiteEmailAddress()
86 return $this->siteEmailAddress;
90 * Gets the site's default sender name
94 public function getSiteEmailName()
96 return $this->siteEmailName;
100 * Creates a new system email
102 * @return SystemMailBuilder
104 public function newSystemMail()
106 return new SystemMailBuilder($this->l10n, $this->baseUrl, $this->config, $this->logger,
107 $this->getSiteEmailAddress(), $this->getSiteEmailName());
111 * Creates a new mail for notifications
113 * @return NotifyMailBuilder
115 public function newNotifyMail()
117 return new NotifyMailBuilder($this->l10n, $this->baseUrl, $this->config, $this->logger,
118 $this->getSiteEmailAddress(), $this->getSiteEmailName());
122 * Send a multipart/alternative message with Text and HTML versions
124 * @param IEmail $email The email to send
127 * @throws InternalServerErrorException
129 public function send(IEmail $email)
131 Hook::callAll('emailer_send_prepare', $email);
137 // @see https://github.com/friendica/friendica/issues/9142
139 foreach ($email->getAdditionalMailHeader() as $name => $value) {
140 if (strtolower($name) == 'message-id') {
141 $countMessageId += count($value);
144 if ($countMessageId > 1) {
145 $this->logger->warning('More than one Message-ID found - RFC violation', ['email' => $email]);
148 $email_textonly = false;
149 if (!empty($email->getRecipientUid())) {
150 $email_textonly = $this->pConfig->get($email->getRecipientUid(), 'system', 'email_textonly');
153 $fromName = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
154 $fromAddress = $email->getFromAddress();
155 $replyTo = $email->getReplyTo();
156 $messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
158 // generate a mime boundary
159 $mimeBoundary = rand(0, 9) . '-'
160 . rand(100000000, 999999999) . '-'
161 . rand(100000000, 999999999) . '=:'
162 . rand(10000, 99999);
164 // generate a multipart/alternative message header
165 $messageHeader = $email->getAdditionalMailHeaderString() .
166 "From: $fromName <{$fromAddress}>\n" .
167 "Reply-To: $fromName <{$replyTo}>\n" .
168 "MIME-Version: 1.0\n" .
169 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
171 // assemble the final multipart message body with the text and html types included
172 $textBody = chunk_split(base64_encode($email->getMessage(true)));
173 $htmlBody = chunk_split(base64_encode($email->getMessage()));
174 $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
175 "Content-Type: text/plain; charset=UTF-8\n" .
176 "Content-Transfer-Encoding: base64\n\n" .
179 if (!$email_textonly && !is_null($email->getMessage())) {
180 $multipartMessageBody .=
181 "--" . $mimeBoundary . "\n" . // text/html section
182 "Content-Type: text/html; charset=UTF-8\n" .
183 "Content-Transfer-Encoding: base64\n\n" .
186 $multipartMessageBody .=
187 "--" . $mimeBoundary . "--\n"; // message ending
189 if ($this->config->get('system', 'sendmail_params', true)) {
190 $sendmail_params = '-f ' . $fromAddress;
192 $sendmail_params = null;
197 'to' => $email->getToAddress(),
198 'subject' => $messageSubject,
199 'body' => $multipartMessageBody,
200 'headers' => $messageHeader,
201 'parameters' => $sendmail_params,
205 Hook::callAll('emailer_send', $hookdata);
207 if ($hookdata['sent']) {
213 $hookdata['subject'],
215 $hookdata['headers'],
216 $hookdata['parameters']
219 $this->logger->debug('header ' . 'To: ' . $email->getToAddress() . '\n' . $messageHeader);
220 $this->logger->debug('return value ' . (($res) ? 'true' : 'false'));
226 * Wrapper around the mail() method (mainly used to overwrite for tests)
229 * @param string $to Recipient of this mail
230 * @param string $subject Subject of this mail
231 * @param string $body Message body of this mail
232 * @param string $headers Headers of this mail
233 * @param string $parameters Additional (sendmail) parameters of this mail
235 * @return boolean true if the mail was successfully accepted for delivery, false otherwise.
237 protected function mail(string $to, string $subject, string $body, string $headers, string $parameters)
239 return mail($to, $subject, $body, $headers, $parameters);