3 * @file src/Util/Emailer.php
5 namespace Friendica\Util;
8 use Friendica\Core\Config\IConfig;
9 use Friendica\Core\Hook;
10 use Friendica\Core\L10n;
11 use Friendica\Core\PConfig\IPConfig;
12 use Friendica\Network\HTTPException\InternalServerErrorException;
13 use Friendica\Object\EMail\IEmail;
14 use Friendica\Protocol\Email;
15 use Friendica\Util\EMailer\NotifyMailBuilder;
16 use Friendica\Util\EMailer\SystemMailBuilder;
17 use Psr\Log\LoggerInterface;
20 * class to handle emailing
28 /** @var LoggerInterface */
30 /** @var App\BaseURL */
36 private $siteEmailAddress;
38 private $siteEmailName;
40 public function __construct(IConfig $config, IPConfig $pConfig, App\BaseURL $baseURL, LoggerInterface $logger,
43 $this->config = $config;
44 $this->pConfig = $pConfig;
45 $this->logger = $logger;
46 $this->baseUrl = $baseURL;
47 $this->l10n = $defaultLang;
49 $this->siteEmailAddress = $this->config->get('config', 'sender_email');
50 if (empty($sysEmailAddress)) {
51 $hostname = $this->baseUrl->getHostname();
52 if (strpos($hostname, ':')) {
53 $hostname = substr($hostname, 0, strpos($hostname, ':'));
56 $this->siteEmailAddress = 'noreply@' . $hostname;
59 $this->siteEmailName = $this->config->get('config', 'sitename', 'Friendica Social Network');
63 * Gets the site's default sender email address
67 public function getSiteEmailAddress()
69 return $this->siteEmailAddress;
73 * Gets the site's default sender name
77 public function getSiteEmailName()
79 return $this->siteEmailName;
83 * Creates a new system email
85 * @return SystemMailBuilder
87 public function newSystemMail()
89 return new SystemMailBuilder($this->l10n, $this->baseUrl, $this->config, $this->logger,
90 $this->getSiteEmailAddress(), $this->getSiteEmailName());
94 * Creates a new mail for notifications
96 * @return NotifyMailBuilder
98 public function newNotifyMail()
100 return new NotifyMailBuilder($this->l10n, $this->baseUrl, $this->config, $this->logger,
101 $this->getSiteEmailAddress(), $this->getSiteEmailName());
105 * Send a multipart/alternative message with Text and HTML versions
107 * @param IEmail $email The email to send
110 * @throws InternalServerErrorException
112 public function send(IEmail $email)
114 Hook::callAll('emailer_send_prepare', $email);
120 $email_textonly = false;
121 if (!empty($email->getRecipientUid())) {
122 $email_textonly = $this->pConfig->get($email->getRecipientUid(), 'system', 'email_textonly');
125 $fromName = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
126 $fromAddress = $email->getFromAddress();
127 $replyTo = $email->getReplyTo();
128 $messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
130 // generate a mime boundary
131 $mimeBoundary = rand(0, 9) . '-'
132 . rand(100000000, 999999999) . '-'
133 . rand(100000000, 999999999) . '=:'
134 . rand(10000, 99999);
136 // generate a multipart/alternative message header
137 $messageHeader = $email->getAdditionalMailHeader() .
138 "From: $fromName <{$fromAddress}>\n" .
139 "Reply-To: $fromName <{$replyTo}>\n" .
140 "MIME-Version: 1.0\n" .
141 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
143 // assemble the final multipart message body with the text and html types included
144 $textBody = chunk_split(base64_encode($email->getMessage(true)));
145 $htmlBody = chunk_split(base64_encode($email->getMessage()));
146 $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
147 "Content-Type: text/plain; charset=UTF-8\n" .
148 "Content-Transfer-Encoding: base64\n\n" .
151 if (!$email_textonly && !is_null($email->getMessage())) {
152 $multipartMessageBody .=
153 "--" . $mimeBoundary . "\n" . // text/html section
154 "Content-Type: text/html; charset=UTF-8\n" .
155 "Content-Transfer-Encoding: base64\n\n" .
158 $multipartMessageBody .=
159 "--" . $mimeBoundary . "--\n"; // message ending
161 if ($this->config->get('system', 'sendmail_params', true)) {
162 $sendmail_params = '-f ' . $fromAddress;
164 $sendmail_params = null;
169 'to' => $email->getToAddress(),
170 'subject' => $messageSubject,
171 'body' => $multipartMessageBody,
172 'headers' => $messageHeader,
173 'parameters' => $sendmail_params,
177 Hook::callAll('emailer_send', $hookdata);
179 if ($hookdata['sent']) {
185 $hookdata['subject'],
187 $hookdata['headers'],
188 $hookdata['parameters']
190 $this->logger->debug('header ' . 'To: ' . $email->getToAddress() . '\n' . $messageHeader);
191 $this->logger->debug('return value ' . (($res) ? 'true' : 'false'));