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 */
32 public function __construct(IConfig $config, IPConfig $pConfig, App\BaseURL $baseURL, LoggerInterface $logger)
34 $this->config = $config;
35 $this->pConfig = $pConfig;
36 $this->logger = $logger;
37 $this->baseUrl = $baseURL;
41 * Creates a new system email
43 * @param App $a The Friendica app
44 * @param L10n $l10n The chosen language for the new email
46 * @return SystemMailBuilder
48 public function newSystemMail(App $a, L10n $l10n)
50 return new SystemMailBuilder($a, $l10n, $this->baseUrl, $this->config);
54 * Send a multipart/alternative message with Text and HTML versions
56 * @param IEmail $email The email to send
59 * @throws InternalServerErrorException
61 public function send(IEmail $email)
63 Hook::callAll('emailer_send_prepare', $email);
69 $email_textonly = false;
70 if (!empty($email->getRecipientUid())) {
71 $email_textonly = $this->pConfig->get($email->getRecipientUid(), 'system', 'email_textonly');
74 $fromName = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
75 $fromAddress = $email->getFromAddress();
76 $replyTo = $email->getReplyTo();
77 $messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
79 // generate a mime boundary
80 $mimeBoundary = rand(0, 9) . '-'
81 . rand(100000000, 999999999) . '-'
82 . rand(100000000, 999999999) . '=:'
85 // generate a multipart/alternative message header
86 $messageHeader = $email->getAdditionalMailHeader() .
87 "From: $fromName <{$fromAddress}>\n" .
88 "Reply-To: $fromName <{$replyTo}>\n" .
89 "MIME-Version: 1.0\n" .
90 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
92 // assemble the final multipart message body with the text and html types included
93 $textBody = chunk_split(base64_encode($email->getMessage(true)));
94 $htmlBody = chunk_split(base64_encode($email->getMessage()));
95 $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
96 "Content-Type: text/plain; charset=UTF-8\n" .
97 "Content-Transfer-Encoding: base64\n\n" .
100 if (!$email_textonly && !is_null($email->getMessage())) {
101 $multipartMessageBody .=
102 "--" . $mimeBoundary . "\n" . // text/html section
103 "Content-Type: text/html; charset=UTF-8\n" .
104 "Content-Transfer-Encoding: base64\n\n" .
107 $multipartMessageBody .=
108 "--" . $mimeBoundary . "--\n"; // message ending
110 if ($this->config->get('system', 'sendmail_params', true)) {
111 $sendmail_params = '-f ' . $fromAddress;
113 $sendmail_params = null;
118 'to' => $email->getToAddress(),
119 'subject' => $messageSubject,
120 'body' => $multipartMessageBody,
121 'headers' => $messageHeader,
122 'parameters' => $sendmail_params,
126 Hook::callAll('emailer_send', $hookdata);
128 if ($hookdata['sent']) {
134 $hookdata['subject'],
136 $hookdata['headers'],
137 $hookdata['parameters']
139 $this->logger->debug('header ' . 'To: ' . $email->getToAddress() . '\n' . $messageHeader);
140 $this->logger->debug('return value ' . (($res) ? 'true' : 'false'));