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