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\Model\Notify;
13 use Friendica\Network\HTTPException\InternalServerErrorException;
14 use Friendica\Object\EMail\IEmail;
15 use Friendica\Protocol\Email;
16 use Friendica\Util\EMailer\NotifyMailBuilder;
17 use Friendica\Util\EMailer\SystemMailBuilder;
18 use Psr\Log\LoggerInterface;
21 * class to handle emailing
29 /** @var LoggerInterface */
31 /** @var App\BaseURL */
37 private $siteEmailAddress;
39 private $siteEmailName;
41 public function __construct(IConfig $config, IPConfig $pConfig, App\BaseURL $baseURL, LoggerInterface $logger,
44 $this->config = $config;
45 $this->pConfig = $pConfig;
46 $this->logger = $logger;
47 $this->baseUrl = $baseURL;
48 $this->l10n = $defaultLang;
50 $this->siteEmailAddress = $this->config->get('config', 'sender_email');
51 if (empty($sysEmailAddress)) {
52 $hostname = $this->baseUrl->getHostname();
53 if (strpos($hostname, ':')) {
54 $hostname = substr($hostname, 0, strpos($hostname, ':'));
57 $this->siteEmailAddress = 'noreply@' . $hostname;
60 $this->siteEmailName = $this->config->get('config', 'sitename', 'Friendica Social Network');
64 * Gets the site's default sender email address
68 public function getSiteEmailAddress()
70 return $this->siteEmailAddress;
74 * Gets the site's default sender name
78 public function getSiteEmailName()
80 return $this->siteEmailName;
84 * Creates a new system email
86 * @return SystemMailBuilder
88 public function newSystemMail()
90 return new SystemMailBuilder($this->l10n, $this->baseUrl, $this->config, $this->logger,
91 $this->getSiteEmailAddress(), $this->getSiteEmailName());
95 * Creates a new mail for notifications
99 * @return NotifyMailBuilder
101 public function newNotifyMail()
103 return new NotifyMailBuilder($this->l10n, $this->baseUrl, $this->config, $this->logger,
104 $this->getSiteEmailAddress(), $this->getSiteEmailName());
108 * Send a multipart/alternative message with Text and HTML versions
110 * @param IEmail $email The email to send
113 * @throws InternalServerErrorException
115 public function send(IEmail $email)
117 Hook::callAll('emailer_send_prepare', $email);
123 $email_textonly = false;
124 if (!empty($email->getRecipientUid())) {
125 $email_textonly = $this->pConfig->get($email->getRecipientUid(), 'system', 'email_textonly');
128 $fromName = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
129 $fromAddress = $email->getFromAddress();
130 $replyTo = $email->getReplyTo();
131 $messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
133 // generate a mime boundary
134 $mimeBoundary = rand(0, 9) . '-'
135 . rand(100000000, 999999999) . '-'
136 . rand(100000000, 999999999) . '=:'
137 . rand(10000, 99999);
139 // generate a multipart/alternative message header
140 $messageHeader = $email->getAdditionalMailHeader() .
141 "From: $fromName <{$fromAddress}>\n" .
142 "Reply-To: $fromName <{$replyTo}>\n" .
143 "MIME-Version: 1.0\n" .
144 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
146 // assemble the final multipart message body with the text and html types included
147 $textBody = chunk_split(base64_encode($email->getMessage(true)));
148 $htmlBody = chunk_split(base64_encode($email->getMessage()));
149 $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
150 "Content-Type: text/plain; charset=UTF-8\n" .
151 "Content-Transfer-Encoding: base64\n\n" .
154 if (!$email_textonly && !is_null($email->getMessage())) {
155 $multipartMessageBody .=
156 "--" . $mimeBoundary . "\n" . // text/html section
157 "Content-Type: text/html; charset=UTF-8\n" .
158 "Content-Transfer-Encoding: base64\n\n" .
161 $multipartMessageBody .=
162 "--" . $mimeBoundary . "--\n"; // message ending
164 if ($this->config->get('system', 'sendmail_params', true)) {
165 $sendmail_params = '-f ' . $fromAddress;
167 $sendmail_params = null;
172 'to' => $email->getToAddress(),
173 'subject' => $messageSubject,
174 'body' => $multipartMessageBody,
175 'headers' => $messageHeader,
176 'parameters' => $sendmail_params,
180 Hook::callAll('emailer_send', $hookdata);
182 if ($hookdata['sent']) {
188 $hookdata['subject'],
190 $hookdata['headers'],
191 $hookdata['parameters']
193 $this->logger->debug('header ' . 'To: ' . $email->getToAddress() . '\n' . $messageHeader);
194 $this->logger->debug('return value ' . (($res) ? 'true' : 'false'));