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($sysEmailAddress)) {
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 $email_textonly = false;
138 if (!empty($email->getRecipientUid())) {
139 $email_textonly = $this->pConfig->get($email->getRecipientUid(), 'system', 'email_textonly');
142 $fromName = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
143 $fromAddress = $email->getFromAddress();
144 $replyTo = $email->getReplyTo();
145 $messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
147 // generate a mime boundary
148 $mimeBoundary = rand(0, 9) . '-'
149 . rand(100000000, 999999999) . '-'
150 . rand(100000000, 999999999) . '=:'
151 . rand(10000, 99999);
153 // generate a multipart/alternative message header
154 $messageHeader = $email->getAdditionalMailHeader() .
155 "From: $fromName <{$fromAddress}>\n" .
156 "Reply-To: $fromName <{$replyTo}>\n" .
157 "MIME-Version: 1.0\n" .
158 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
160 // assemble the final multipart message body with the text and html types included
161 $textBody = chunk_split(base64_encode($email->getMessage(true)));
162 $htmlBody = chunk_split(base64_encode($email->getMessage()));
163 $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
164 "Content-Type: text/plain; charset=UTF-8\n" .
165 "Content-Transfer-Encoding: base64\n\n" .
168 if (!$email_textonly && !is_null($email->getMessage())) {
169 $multipartMessageBody .=
170 "--" . $mimeBoundary . "\n" . // text/html section
171 "Content-Type: text/html; charset=UTF-8\n" .
172 "Content-Transfer-Encoding: base64\n\n" .
175 $multipartMessageBody .=
176 "--" . $mimeBoundary . "--\n"; // message ending
178 if ($this->config->get('system', 'sendmail_params', true)) {
179 $sendmail_params = '-f ' . $fromAddress;
181 $sendmail_params = null;
186 'to' => $email->getToAddress(),
187 'subject' => $messageSubject,
188 'body' => $multipartMessageBody,
189 'headers' => $messageHeader,
190 'parameters' => $sendmail_params,
194 Hook::callAll('emailer_send', $hookdata);
196 if ($hookdata['sent']) {
202 $hookdata['subject'],
204 $hookdata['headers'],
205 $hookdata['parameters']
207 $this->logger->debug('header ' . 'To: ' . $email->getToAddress() . '\n' . $messageHeader);
208 $this->logger->debug('return value ' . (($res) ? 'true' : 'false'));