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 * @param L10n $l10n The chosen language for the new email
101 * @return NotifyMailBuilder
103 public function newNotifyMail(L10n $l10n)
105 return new NotifyMailBuilder($l10n, $this->baseUrl, $this->config,
106 $this->getSiteEmailAddress(), $this->getSiteEmailName());
110 * Send a multipart/alternative message with Text and HTML versions
112 * @param IEmail $email The email to send
115 * @throws InternalServerErrorException
117 public function send(IEmail $email)
119 Hook::callAll('emailer_send_prepare', $email);
125 $email_textonly = false;
126 if (!empty($email->getRecipientUid())) {
127 $email_textonly = $this->pConfig->get($email->getRecipientUid(), 'system', 'email_textonly');
130 $fromName = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
131 $fromAddress = $email->getFromAddress();
132 $replyTo = $email->getReplyTo();
133 $messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
135 // generate a mime boundary
136 $mimeBoundary = rand(0, 9) . '-'
137 . rand(100000000, 999999999) . '-'
138 . rand(100000000, 999999999) . '=:'
139 . rand(10000, 99999);
141 // generate a multipart/alternative message header
142 $messageHeader = $email->getAdditionalMailHeader() .
143 "From: $fromName <{$fromAddress}>\n" .
144 "Reply-To: $fromName <{$replyTo}>\n" .
145 "MIME-Version: 1.0\n" .
146 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
148 // assemble the final multipart message body with the text and html types included
149 $textBody = chunk_split(base64_encode($email->getMessage(true)));
150 $htmlBody = chunk_split(base64_encode($email->getMessage()));
151 $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
152 "Content-Type: text/plain; charset=UTF-8\n" .
153 "Content-Transfer-Encoding: base64\n\n" .
156 if (!$email_textonly && !is_null($email->getMessage())) {
157 $multipartMessageBody .=
158 "--" . $mimeBoundary . "\n" . // text/html section
159 "Content-Type: text/html; charset=UTF-8\n" .
160 "Content-Transfer-Encoding: base64\n\n" .
163 $multipartMessageBody .=
164 "--" . $mimeBoundary . "--\n"; // message ending
166 if ($this->config->get('system', 'sendmail_params', true)) {
167 $sendmail_params = '-f ' . $fromAddress;
169 $sendmail_params = null;
174 'to' => $email->getToAddress(),
175 'subject' => $messageSubject,
176 'body' => $multipartMessageBody,
177 'headers' => $messageHeader,
178 'parameters' => $sendmail_params,
182 Hook::callAll('emailer_send', $hookdata);
184 if ($hookdata['sent']) {
190 $hookdata['subject'],
192 $hookdata['headers'],
193 $hookdata['parameters']
195 $this->logger->debug('header ' . 'To: ' . $email->getToAddress() . '\n' . $messageHeader);
196 $this->logger->debug('return value ' . (($res) ? 'true' : 'false'));