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