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 */
33 private $siteEmailAddress;
35 private $siteEmailName;
37 public function __construct(IConfig $config, IPConfig $pConfig, App\BaseURL $baseURL, LoggerInterface $logger)
39 $this->config = $config;
40 $this->pConfig = $pConfig;
41 $this->logger = $logger;
42 $this->baseUrl = $baseURL;
44 $this->siteEmailAddress = $this->config->get('config', 'sender_email');
45 if (empty($sysEmailAddress)) {
46 $hostname = $this->baseUrl->getHostname();
47 if (strpos($hostname, ':')) {
48 $hostname = substr($hostname, 0, strpos($hostname, ':'));
51 $this->siteEmailAddress = 'noreply@' . $hostname;
54 $this->siteEmailName = $this->config->get('config', 'sitename', 'Friendica Social Network');
58 * Gets the site's default sender email address
62 public function getSiteEmailAddress()
64 return $this->siteEmailAddress;
68 * Gets the site's default sender name
72 public function getSiteEmailName()
74 return $this->siteEmailName;
78 * Creates a new system email
80 * @param L10n $l10n The chosen language for the new email
82 * @return SystemMailBuilder
84 public function newSystemMail(L10n $l10n)
86 return new SystemMailBuilder($l10n, $this->baseUrl, $this->config,
87 $this->getSiteEmailAddress(), $this->getSiteEmailName());
91 * Send a multipart/alternative message with Text and HTML versions
93 * @param IEmail $email The email to send
96 * @throws InternalServerErrorException
98 public function send(IEmail $email)
100 Hook::callAll('emailer_send_prepare', $email);
106 $email_textonly = false;
107 if (!empty($email->getRecipientUid())) {
108 $email_textonly = $this->pConfig->get($email->getRecipientUid(), 'system', 'email_textonly');
111 $fromName = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
112 $fromAddress = $email->getFromAddress();
113 $replyTo = $email->getReplyTo();
114 $messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
116 // generate a mime boundary
117 $mimeBoundary = rand(0, 9) . '-'
118 . rand(100000000, 999999999) . '-'
119 . rand(100000000, 999999999) . '=:'
120 . rand(10000, 99999);
122 // generate a multipart/alternative message header
123 $messageHeader = $email->getAdditionalMailHeader() .
124 "From: $fromName <{$fromAddress}>\n" .
125 "Reply-To: $fromName <{$replyTo}>\n" .
126 "MIME-Version: 1.0\n" .
127 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
129 // assemble the final multipart message body with the text and html types included
130 $textBody = chunk_split(base64_encode($email->getMessage(true)));
131 $htmlBody = chunk_split(base64_encode($email->getMessage()));
132 $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
133 "Content-Type: text/plain; charset=UTF-8\n" .
134 "Content-Transfer-Encoding: base64\n\n" .
137 if (!$email_textonly && !is_null($email->getMessage())) {
138 $multipartMessageBody .=
139 "--" . $mimeBoundary . "\n" . // text/html section
140 "Content-Type: text/html; charset=UTF-8\n" .
141 "Content-Transfer-Encoding: base64\n\n" .
144 $multipartMessageBody .=
145 "--" . $mimeBoundary . "--\n"; // message ending
147 if ($this->config->get('system', 'sendmail_params', true)) {
148 $sendmail_params = '-f ' . $fromAddress;
150 $sendmail_params = null;
155 'to' => $email->getToAddress(),
156 'subject' => $messageSubject,
157 'body' => $multipartMessageBody,
158 'headers' => $messageHeader,
159 'parameters' => $sendmail_params,
163 Hook::callAll('emailer_send', $hookdata);
165 if ($hookdata['sent']) {
171 $hookdata['subject'],
173 $hookdata['headers'],
174 $hookdata['parameters']
176 $this->logger->debug('header ' . 'To: ' . $email->getToAddress() . '\n' . $messageHeader);
177 $this->logger->debug('return value ' . (($res) ? 'true' : 'false'));