3 * @copyright Copyright (C) 2020, Friendica
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\IConfig;
26 use Friendica\Core\Hook;
27 use Friendica\Core\L10n;
28 use Friendica\Core\PConfig\IPConfig;
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
45 /** @var LoggerInterface */
47 /** @var App\BaseURL */
53 private $siteEmailAddress;
55 private $siteEmailName;
57 public function __construct(IConfig $config, IPConfig $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 > 0) {
145 $this->logger->warning('More than one Message-ID found - RFC violation', ['email' => $email]);
149 $email_textonly = false;
150 if (!empty($email->getRecipientUid())) {
151 $email_textonly = $this->pConfig->get($email->getRecipientUid(), 'system', 'email_textonly');
154 $fromName = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
155 $fromAddress = $email->getFromAddress();
156 $replyTo = $email->getReplyTo();
157 $messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
159 // generate a mime boundary
160 $mimeBoundary = rand(0, 9) . '-'
161 . rand(100000000, 999999999) . '-'
162 . rand(100000000, 999999999) . '=:'
163 . rand(10000, 99999);
165 // generate a multipart/alternative message header
166 $messageHeader = $email->getAdditionalMailHeaderString() .
167 "From: $fromName <{$fromAddress}>\n" .
168 "Reply-To: $fromName <{$replyTo}>\n" .
169 "MIME-Version: 1.0\n" .
170 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
172 // assemble the final multipart message body with the text and html types included
173 $textBody = chunk_split(base64_encode($email->getMessage(true)));
174 $htmlBody = chunk_split(base64_encode($email->getMessage()));
175 $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
176 "Content-Type: text/plain; charset=UTF-8\n" .
177 "Content-Transfer-Encoding: base64\n\n" .
180 if (!$email_textonly && !is_null($email->getMessage())) {
181 $multipartMessageBody .=
182 "--" . $mimeBoundary . "\n" . // text/html section
183 "Content-Type: text/html; charset=UTF-8\n" .
184 "Content-Transfer-Encoding: base64\n\n" .
187 $multipartMessageBody .=
188 "--" . $mimeBoundary . "--\n"; // message ending
190 if ($this->config->get('system', 'sendmail_params', true)) {
191 $sendmail_params = '-f ' . $fromAddress;
193 $sendmail_params = null;
198 'to' => $email->getToAddress(),
199 'subject' => $messageSubject,
200 'body' => $multipartMessageBody,
201 'headers' => $messageHeader,
202 'parameters' => $sendmail_params,
206 Hook::callAll('emailer_send', $hookdata);
208 if ($hookdata['sent']) {
214 $hookdata['subject'],
216 $hookdata['headers'],
217 $hookdata['parameters']
220 $this->logger->debug('header ' . 'To: ' . $email->getToAddress() . '\n' . $messageHeader);
221 $this->logger->debug('return value ' . (($res) ? 'true' : 'false'));
227 * Wrapper around the mail() method (mainly used to overwrite for tests)
230 * @param string $to Recipient of this mail
231 * @param string $subject Subject of this mail
232 * @param string $body Message body of this mail
233 * @param string $headers Headers of this mail
234 * @param string $parameters Additional (sendmail) parameters of this mail
236 * @return boolean true if the mail was successfully accepted for delivery, false otherwise.
238 protected function mail(string $to, string $subject, string $body, string $headers, string $parameters)
240 return mail($to, $subject, $body, $headers, $parameters);