]> git.mxchange.org Git - friendica.git/blob - src/Util/Emailer.php
66ebcc307d3341b0284503cfc8812ca71340700c
[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                 $this->logger->debug('start emailing', ['email' => $email]);
49
50                 Hook::callAll('emailer_send_prepare', $email);
51
52                 $this->logger->debug('End Hook call', ['email' => $email]);
53
54                 if (empty($email)) {
55                         return true;
56                 }
57
58                 $email_textonly = false;
59                 if (!empty($email->getRecipientUid())) {
60                         $email_textonly = $this->pConfig->get($email->getRecipientUid(), 'system', 'email_textonly');
61                 }
62
63                 $fromName       = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
64                 $fromAddress      = $email->getFromAddress();
65                 $replyTo        = $email->getReplyTo();
66                 $messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
67
68                 // generate a mime boundary
69                 $mimeBoundary = rand(0, 9) . '-'
70                                 . rand(100000000, 999999999) . '-'
71                                 . rand(100000000, 999999999) . '=:'
72                                 . rand(10000, 99999);
73
74                 // generate a multipart/alternative message header
75                 $messageHeader = $email->getAdditionalMailHeader() .
76                                  "From: $fromName <{$fromAddress}>\n" .
77                                  "Reply-To: $fromName <{$replyTo}>\n" .
78                                  "MIME-Version: 1.0\n" .
79                                  "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
80
81                 // assemble the final multipart message body with the text and html types included
82                 $textBody             = chunk_split(base64_encode($email->getMessage(true)));
83                 $htmlBody             = chunk_split(base64_encode($email->getMessage()));
84                 $multipartMessageBody = "--" . $mimeBoundary . "\n" .                    // plain text section
85                                         "Content-Type: text/plain; charset=UTF-8\n" .
86                                         "Content-Transfer-Encoding: base64\n\n" .
87                                         $textBody . "\n";
88
89                 if (!$email_textonly && !is_null($email->getMessage())) {
90                         $multipartMessageBody .=
91                                 "--" . $mimeBoundary . "\n" .                // text/html section
92                                 "Content-Type: text/html; charset=UTF-8\n" .
93                                 "Content-Transfer-Encoding: base64\n\n" .
94                                 $htmlBody . "\n";
95                 }
96                 $multipartMessageBody .=
97                         "--" . $mimeBoundary . "--\n";                    // message ending
98
99                 if ($this->config->get('system', 'sendmail_params', true)) {
100                         $sendmail_params = '-f ' . $fromAddress;
101                 } else {
102                         $sendmail_params = null;
103                 }
104
105                 // send the message
106                 $hookdata = [
107                         'to'         => $email->getToAddress(),
108                         'subject'    => $messageSubject,
109                         'body'       => $multipartMessageBody,
110                         'headers'    => $messageHeader,
111                         'parameters' => $sendmail_params,
112                         'sent'       => false,
113                 ];
114
115                 Hook::callAll('emailer_send', $hookdata);
116
117                 if ($hookdata['sent']) {
118                         return true;
119                 }
120
121                 $res = mail(
122                         $hookdata['to'],
123                         $hookdata['subject'],
124                         $hookdata['body'],
125                         $hookdata['headers'],
126                         $hookdata['parameters']
127                 );
128                 $this->logger->debug('header ' . 'To: ' . $email->getToAddress() . '\n' . $messageHeader);
129                 $this->logger->debug('return value ' . (($res) ? 'true' : 'false'));
130                 return $res;
131         }
132 }