]> git.mxchange.org Git - friendica.git/blob - src/Util/Emailer.php
a9da5346ddab96c72d15ac1c806d4e36c0b75b18
[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\Model\Notify;
13 use Friendica\Network\HTTPException\InternalServerErrorException;
14 use Friendica\Object\EMail\IEmail;
15 use Friendica\Protocol\Email;
16 use Friendica\Util\EMailer\NotifyMailBuilder;
17 use Friendica\Util\EMailer\SystemMailBuilder;
18 use Psr\Log\LoggerInterface;
19
20 /**
21  * class to handle emailing
22  */
23 class Emailer
24 {
25         /** @var IConfig */
26         private $config;
27         /** @var IPConfig */
28         private $pConfig;
29         /** @var LoggerInterface */
30         private $logger;
31         /** @var App\BaseURL */
32         private $baseUrl;
33         /** @var L10n */
34         private $l10n;
35
36         /** @var string */
37         private $siteEmailAddress;
38         /** @var string */
39         private $siteEmailName;
40
41         public function __construct(IConfig $config, IPConfig $pConfig, App\BaseURL $baseURL, LoggerInterface $logger,
42                                     L10n $defaultLang)
43         {
44                 $this->config      = $config;
45                 $this->pConfig     = $pConfig;
46                 $this->logger      = $logger;
47                 $this->baseUrl     = $baseURL;
48                 $this->l10n        = $defaultLang;
49
50                 $this->siteEmailAddress = $this->config->get('config', 'sender_email');
51                 if (empty($sysEmailAddress)) {
52                         $hostname = $this->baseUrl->getHostname();
53                         if (strpos($hostname, ':')) {
54                                 $hostname = substr($hostname, 0, strpos($hostname, ':'));
55                         }
56
57                         $this->siteEmailAddress = 'noreply@' . $hostname;
58                 }
59
60                 $this->siteEmailName = $this->config->get('config', 'sitename', 'Friendica Social Network');
61         }
62
63         /**
64          * Gets the site's default sender email address
65          *
66          * @return string
67          */
68         public function getSiteEmailAddress()
69         {
70                 return $this->siteEmailAddress;
71         }
72
73         /**
74          * Gets the site's default sender name
75          *
76          * @return string
77          */
78         public function getSiteEmailName()
79         {
80                 return $this->siteEmailName;
81         }
82
83         /**
84          * Creates a new system email
85          *
86          * @return SystemMailBuilder
87          */
88         public function newSystemMail()
89         {
90                 return new SystemMailBuilder($this->l10n, $this->baseUrl, $this->config, $this->logger,
91                         $this->getSiteEmailAddress(), $this->getSiteEmailName());
92         }
93
94         /**
95          * Creates a new mail for notifications
96          *
97          * @see Notify
98          *
99          * @return NotifyMailBuilder
100          */
101         public function newNotifyMail()
102         {
103                 return new NotifyMailBuilder($this->l10n, $this->baseUrl, $this->config, $this->logger,
104                         $this->getSiteEmailAddress(), $this->getSiteEmailName());
105         }
106
107         /**
108          * Send a multipart/alternative message with Text and HTML versions
109          *
110          * @param IEmail $email The email to send
111          *
112          * @return bool
113          * @throws InternalServerErrorException
114          */
115         public function send(IEmail $email)
116         {
117                 Hook::callAll('emailer_send_prepare', $email);
118
119                 if (empty($email)) {
120                         return true;
121                 }
122
123                 $email_textonly = false;
124                 if (!empty($email->getRecipientUid())) {
125                         $email_textonly = $this->pConfig->get($email->getRecipientUid(), 'system', 'email_textonly');
126                 }
127
128                 $fromName       = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
129                 $fromAddress      = $email->getFromAddress();
130                 $replyTo        = $email->getReplyTo();
131                 $messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
132
133                 // generate a mime boundary
134                 $mimeBoundary = rand(0, 9) . '-'
135                                 . rand(100000000, 999999999) . '-'
136                                 . rand(100000000, 999999999) . '=:'
137                                 . rand(10000, 99999);
138
139                 // generate a multipart/alternative message header
140                 $messageHeader = $email->getAdditionalMailHeader() .
141                                  "From: $fromName <{$fromAddress}>\n" .
142                                  "Reply-To: $fromName <{$replyTo}>\n" .
143                                  "MIME-Version: 1.0\n" .
144                                  "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
145
146                 // assemble the final multipart message body with the text and html types included
147                 $textBody             = chunk_split(base64_encode($email->getMessage(true)));
148                 $htmlBody             = chunk_split(base64_encode($email->getMessage()));
149                 $multipartMessageBody = "--" . $mimeBoundary . "\n" .                    // plain text section
150                                         "Content-Type: text/plain; charset=UTF-8\n" .
151                                         "Content-Transfer-Encoding: base64\n\n" .
152                                         $textBody . "\n";
153
154                 if (!$email_textonly && !is_null($email->getMessage())) {
155                         $multipartMessageBody .=
156                                 "--" . $mimeBoundary . "\n" .                // text/html section
157                                 "Content-Type: text/html; charset=UTF-8\n" .
158                                 "Content-Transfer-Encoding: base64\n\n" .
159                                 $htmlBody . "\n";
160                 }
161                 $multipartMessageBody .=
162                         "--" . $mimeBoundary . "--\n";                    // message ending
163
164                 if ($this->config->get('system', 'sendmail_params', true)) {
165                         $sendmail_params = '-f ' . $fromAddress;
166                 } else {
167                         $sendmail_params = null;
168                 }
169
170                 // send the message
171                 $hookdata = [
172                         'to'         => $email->getToAddress(),
173                         'subject'    => $messageSubject,
174                         'body'       => $multipartMessageBody,
175                         'headers'    => $messageHeader,
176                         'parameters' => $sendmail_params,
177                         'sent'       => false,
178                 ];
179
180                 Hook::callAll('emailer_send', $hookdata);
181
182                 if ($hookdata['sent']) {
183                         return true;
184                 }
185
186                 $res = mail(
187                         $hookdata['to'],
188                         $hookdata['subject'],
189                         $hookdata['body'],
190                         $hookdata['headers'],
191                         $hookdata['parameters']
192                 );
193                 $this->logger->debug('header ' . 'To: ' . $email->getToAddress() . '\n' . $messageHeader);
194                 $this->logger->debug('return value ' . (($res) ? 'true' : 'false'));
195                 return $res;
196         }
197 }