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