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