]> git.mxchange.org Git - friendica.git/blob - src/Util/Emailer.php
3f9a491d7677ff71e914d10cb0c701f3d4ec9526
[friendica.git] / src / Util / Emailer.php
1 <?php
2 /**
3  * @file src/Util/Emailer.php
4  */
5 namespace Friendica\Util;
6
7 use Friendica\App;
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;
17
18 /**
19  * class to handle emailing
20  */
21 class Emailer
22 {
23         /** @var IConfig */
24         private $config;
25         /** @var IPConfig */
26         private $pConfig;
27         /** @var LoggerInterface */
28         private $logger;
29         /** @var App\BaseURL */
30         private $baseUrl;
31
32         public function __construct(IConfig $config, IPConfig $pConfig, App\BaseURL $baseURL, LoggerInterface $logger)
33         {
34                 $this->config      = $config;
35                 $this->pConfig     = $pConfig;
36                 $this->logger      = $logger;
37                 $this->baseUrl     = $baseURL;
38         }
39
40         /**
41          * Creates a new system email
42          *
43          * @param App $a The Friendica app
44          * @param L10n $l10n The chosen language for the new email
45          *
46          * @return SystemMailBuilder
47          */
48         public function newSystemMail(App $a, L10n $l10n)
49         {
50                 return new SystemMailBuilder($a, $l10n, $this->baseUrl, $this->config);
51         }
52
53         /**
54          * Send a multipart/alternative message with Text and HTML versions
55          *
56          * @param IEmail $email The email to send
57          *
58          * @return bool
59          * @throws InternalServerErrorException
60          */
61         public function send(IEmail $email)
62         {
63                 Hook::callAll('emailer_send_prepare', $email);
64
65                 if (empty($email)) {
66                         return true;
67                 }
68
69                 $email_textonly = false;
70                 if (!empty($email->getRecipientUid())) {
71                         $email_textonly = $this->pConfig->get($email->getRecipientUid(), 'system', 'email_textonly');
72                 }
73
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');
78
79                 // generate a mime boundary
80                 $mimeBoundary = rand(0, 9) . '-'
81                                 . rand(100000000, 999999999) . '-'
82                                 . rand(100000000, 999999999) . '=:'
83                                 . rand(10000, 99999);
84
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}\"";
91
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" .
98                                         $textBody . "\n";
99
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" .
105                                 $htmlBody . "\n";
106                 }
107                 $multipartMessageBody .=
108                         "--" . $mimeBoundary . "--\n";                    // message ending
109
110                 if ($this->config->get('system', 'sendmail_params', true)) {
111                         $sendmail_params = '-f ' . $fromAddress;
112                 } else {
113                         $sendmail_params = null;
114                 }
115
116                 // send the message
117                 $hookdata = [
118                         'to'         => $email->getToAddress(),
119                         'subject'    => $messageSubject,
120                         'body'       => $multipartMessageBody,
121                         'headers'    => $messageHeader,
122                         'parameters' => $sendmail_params,
123                         'sent'       => false,
124                 ];
125
126                 Hook::callAll('emailer_send', $hookdata);
127
128                 if ($hookdata['sent']) {
129                         return true;
130                 }
131
132                 $res = mail(
133                         $hookdata['to'],
134                         $hookdata['subject'],
135                         $hookdata['body'],
136                         $hookdata['headers'],
137                         $hookdata['parameters']
138                 );
139                 $this->logger->debug('header ' . 'To: ' . $email->getToAddress() . '\n' . $messageHeader);
140                 $this->logger->debug('return value ' . (($res) ? 'true' : 'false'));
141                 return $res;
142         }
143 }