]> git.mxchange.org Git - friendica.git/blob - src/Util/Emailer.php
Introduce NotifyEmailBuilder
[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          * @param L10n $l10n The chosen language for the new email
100          *
101          * @return NotifyMailBuilder
102          */
103         public function newNotifyMail(L10n $l10n)
104         {
105                 return new NotifyMailBuilder($l10n, $this->baseUrl, $this->config,
106                         $this->getSiteEmailAddress(), $this->getSiteEmailName());
107         }
108
109         /**
110          * Send a multipart/alternative message with Text and HTML versions
111          *
112          * @param IEmail $email The email to send
113          *
114          * @return bool
115          * @throws InternalServerErrorException
116          */
117         public function send(IEmail $email)
118         {
119                 Hook::callAll('emailer_send_prepare', $email);
120
121                 if (empty($email)) {
122                         return true;
123                 }
124
125                 $email_textonly = false;
126                 if (!empty($email->getRecipientUid())) {
127                         $email_textonly = $this->pConfig->get($email->getRecipientUid(), 'system', 'email_textonly');
128                 }
129
130                 $fromName       = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
131                 $fromAddress      = $email->getFromAddress();
132                 $replyTo        = $email->getReplyTo();
133                 $messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
134
135                 // generate a mime boundary
136                 $mimeBoundary = rand(0, 9) . '-'
137                                 . rand(100000000, 999999999) . '-'
138                                 . rand(100000000, 999999999) . '=:'
139                                 . rand(10000, 99999);
140
141                 // generate a multipart/alternative message header
142                 $messageHeader = $email->getAdditionalMailHeader() .
143                                  "From: $fromName <{$fromAddress}>\n" .
144                                  "Reply-To: $fromName <{$replyTo}>\n" .
145                                  "MIME-Version: 1.0\n" .
146                                  "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
147
148                 // assemble the final multipart message body with the text and html types included
149                 $textBody             = chunk_split(base64_encode($email->getMessage(true)));
150                 $htmlBody             = chunk_split(base64_encode($email->getMessage()));
151                 $multipartMessageBody = "--" . $mimeBoundary . "\n" .                    // plain text section
152                                         "Content-Type: text/plain; charset=UTF-8\n" .
153                                         "Content-Transfer-Encoding: base64\n\n" .
154                                         $textBody . "\n";
155
156                 if (!$email_textonly && !is_null($email->getMessage())) {
157                         $multipartMessageBody .=
158                                 "--" . $mimeBoundary . "\n" .                // text/html section
159                                 "Content-Type: text/html; charset=UTF-8\n" .
160                                 "Content-Transfer-Encoding: base64\n\n" .
161                                 $htmlBody . "\n";
162                 }
163                 $multipartMessageBody .=
164                         "--" . $mimeBoundary . "--\n";                    // message ending
165
166                 if ($this->config->get('system', 'sendmail_params', true)) {
167                         $sendmail_params = '-f ' . $fromAddress;
168                 } else {
169                         $sendmail_params = null;
170                 }
171
172                 // send the message
173                 $hookdata = [
174                         'to'         => $email->getToAddress(),
175                         'subject'    => $messageSubject,
176                         'body'       => $multipartMessageBody,
177                         'headers'    => $messageHeader,
178                         'parameters' => $sendmail_params,
179                         'sent'       => false,
180                 ];
181
182                 Hook::callAll('emailer_send', $hookdata);
183
184                 if ($hookdata['sent']) {
185                         return true;
186                 }
187
188                 $res = mail(
189                         $hookdata['to'],
190                         $hookdata['subject'],
191                         $hookdata['body'],
192                         $hookdata['headers'],
193                         $hookdata['parameters']
194                 );
195                 $this->logger->debug('header ' . 'To: ' . $email->getToAddress() . '\n' . $messageHeader);
196                 $this->logger->debug('return value ' . (($res) ? 'true' : 'false'));
197                 return $res;
198         }
199 }