]> git.mxchange.org Git - friendica.git/blob - src/Util/Emailer.php
Merge pull request #8212 from MrPetovan/task/release-cache-buster
[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\PConfig\IPConfig;
11 use Friendica\Network\HTTPException\InternalServerErrorException;
12 use Friendica\Object\EMail\IEmail;
13 use Friendica\Protocol\Email;
14 use Psr\Log\LoggerInterface;
15
16 /**
17  * class to handle emailing
18  */
19 class Emailer
20 {
21         /** @var IConfig */
22         private $config;
23         /** @var IPConfig */
24         private $pConfig;
25         /** @var LoggerInterface */
26         private $logger;
27         /** @var App\BaseURL */
28         private $baseUrl;
29
30         public function __construct(IConfig $config, IPConfig $pConfig, App\BaseURL $baseURL, LoggerInterface $logger)
31         {
32                 $this->config      = $config;
33                 $this->pConfig     = $pConfig;
34                 $this->logger      = $logger;
35                 $this->baseUrl     = $baseURL;
36         }
37
38         /**
39          * Send a multipart/alternative message with Text and HTML versions
40          *
41          * @param IEmail $email The email to send
42          *
43          * @return bool
44          * @throws InternalServerErrorException
45          */
46         public function send(IEmail $email)
47         {
48                 Hook::callAll('emailer_send_prepare', $email);
49
50                 if (empty($email)) {
51                         return true;
52                 }
53
54                 $email_textonly = false;
55                 if (!empty($email->getRecipientUid())) {
56                         $email_textonly = $this->pConfig->get($email->getRecipientUid(), 'system', 'email_textonly');
57                 }
58
59                 $fromName       = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
60                 $fromAddress      = $email->getFromAddress();
61                 $replyTo        = $email->getReplyTo();
62                 $messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
63
64                 // generate a mime boundary
65                 $mimeBoundary = rand(0, 9) . '-'
66                                 . rand(100000000, 999999999) . '-'
67                                 . rand(100000000, 999999999) . '=:'
68                                 . rand(10000, 99999);
69
70                 // generate a multipart/alternative message header
71                 $messageHeader = $email->getAdditionalMailHeader() .
72                                  "From: $fromName <{$fromAddress}>\n" .
73                                  "Reply-To: $fromName <{$replyTo}>\n" .
74                                  "MIME-Version: 1.0\n" .
75                                  "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
76
77                 // assemble the final multipart message body with the text and html types included
78                 $textBody             = chunk_split(base64_encode($email->getMessage(true)));
79                 $htmlBody             = chunk_split(base64_encode($email->getMessage()));
80                 $multipartMessageBody = "--" . $mimeBoundary . "\n" .                    // plain text section
81                                         "Content-Type: text/plain; charset=UTF-8\n" .
82                                         "Content-Transfer-Encoding: base64\n\n" .
83                                         $textBody . "\n";
84
85                 if (!$email_textonly && !is_null($email->getMessage())) {
86                         $multipartMessageBody .=
87                                 "--" . $mimeBoundary . "\n" .                // text/html section
88                                 "Content-Type: text/html; charset=UTF-8\n" .
89                                 "Content-Transfer-Encoding: base64\n\n" .
90                                 $htmlBody . "\n";
91                 }
92                 $multipartMessageBody .=
93                         "--" . $mimeBoundary . "--\n";                    // message ending
94
95                 if ($this->config->get('system', 'sendmail_params', true)) {
96                         $sendmail_params = '-f ' . $fromAddress;
97                 } else {
98                         $sendmail_params = null;
99                 }
100
101                 // send the message
102                 $hookdata = [
103                         'to'         => $email->getToAddress(),
104                         'subject'    => $messageSubject,
105                         'body'       => $multipartMessageBody,
106                         'headers'    => $messageHeader,
107                         'parameters' => $sendmail_params,
108                         'sent'       => false,
109                 ];
110
111                 Hook::callAll('emailer_send', $hookdata);
112
113                 if ($hookdata['sent']) {
114                         return true;
115                 }
116
117                 $res = mail(
118                         $hookdata['to'],
119                         $hookdata['subject'],
120                         $hookdata['body'],
121                         $hookdata['headers'],
122                         $hookdata['parameters']
123                 );
124                 $this->logger->debug('header ' . 'To: ' . $email->getToAddress() . '\n' . $messageHeader);
125                 $this->logger->debug('return value ' . (($res) ? 'true' : 'false'));
126                 return $res;
127         }
128 }