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\SystemMailBuilder;
16 use Psr\Log\LoggerInterface;
19 * class to handle emailing
27 /** @var LoggerInterface */
29 /** @var App\BaseURL */
35 private $siteEmailAddress;
37 private $siteEmailName;
39 public function __construct(IConfig $config, IPConfig $pConfig, App\BaseURL $baseURL, LoggerInterface $logger,
42 $this->config = $config;
43 $this->pConfig = $pConfig;
44 $this->logger = $logger;
45 $this->baseUrl = $baseURL;
46 $this->l10n = $defaultLang;
48 $this->siteEmailAddress = $this->config->get('config', 'sender_email');
49 if (empty($sysEmailAddress)) {
50 $hostname = $this->baseUrl->getHostname();
51 if (strpos($hostname, ':')) {
52 $hostname = substr($hostname, 0, strpos($hostname, ':'));
55 $this->siteEmailAddress = 'noreply@' . $hostname;
58 $this->siteEmailName = $this->config->get('config', 'sitename', 'Friendica Social Network');
62 * Gets the site's default sender email address
66 public function getSiteEmailAddress()
68 return $this->siteEmailAddress;
72 * Gets the site's default sender name
76 public function getSiteEmailName()
78 return $this->siteEmailName;
82 * Creates a new system email
84 * @return SystemMailBuilder
86 public function newSystemMail()
88 return new SystemMailBuilder($this->l10n, $this->baseUrl, $this->config, $this->logger,
89 $this->getSiteEmailAddress(), $this->getSiteEmailName());
93 * Send a multipart/alternative message with Text and HTML versions
95 * @param IEmail $email The email to send
98 * @throws InternalServerErrorException
100 public function send(IEmail $email)
102 Hook::callAll('emailer_send_prepare', $email);
108 $email_textonly = false;
109 if (!empty($email->getRecipientUid())) {
110 $email_textonly = $this->pConfig->get($email->getRecipientUid(), 'system', 'email_textonly');
113 $fromName = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
114 $fromAddress = $email->getFromAddress();
115 $replyTo = $email->getReplyTo();
116 $messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
118 // generate a mime boundary
119 $mimeBoundary = rand(0, 9) . '-'
120 . rand(100000000, 999999999) . '-'
121 . rand(100000000, 999999999) . '=:'
122 . rand(10000, 99999);
124 // generate a multipart/alternative message header
125 $messageHeader = $email->getAdditionalMailHeader() .
126 "From: $fromName <{$fromAddress}>\n" .
127 "Reply-To: $fromName <{$replyTo}>\n" .
128 "MIME-Version: 1.0\n" .
129 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
131 // assemble the final multipart message body with the text and html types included
132 $textBody = chunk_split(base64_encode($email->getMessage(true)));
133 $htmlBody = chunk_split(base64_encode($email->getMessage()));
134 $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
135 "Content-Type: text/plain; charset=UTF-8\n" .
136 "Content-Transfer-Encoding: base64\n\n" .
139 if (!$email_textonly && !is_null($email->getMessage())) {
140 $multipartMessageBody .=
141 "--" . $mimeBoundary . "\n" . // text/html section
142 "Content-Type: text/html; charset=UTF-8\n" .
143 "Content-Transfer-Encoding: base64\n\n" .
146 $multipartMessageBody .=
147 "--" . $mimeBoundary . "--\n"; // message ending
149 if ($this->config->get('system', 'sendmail_params', true)) {
150 $sendmail_params = '-f ' . $fromAddress;
152 $sendmail_params = null;
157 'to' => $email->getToAddress(),
158 'subject' => $messageSubject,
159 'body' => $multipartMessageBody,
160 'headers' => $messageHeader,
161 'parameters' => $sendmail_params,
165 Hook::callAll('emailer_send', $hookdata);
167 if ($hookdata['sent']) {
173 $hookdata['subject'],
175 $hookdata['headers'],
176 $hookdata['parameters']
178 $this->logger->debug('header ' . 'To: ' . $email->getToAddress() . '\n' . $messageHeader);
179 $this->logger->debug('return value ' . (($res) ? 'true' : 'false'));