]> git.mxchange.org Git - friendica.git/blob - src/Util/Emailer.php
7017680745a95e86dc31b65e78011918868f2832
[friendica.git] / src / Util / Emailer.php
1 <?php
2 /**
3  * @file src/Util/Emailer.php
4  */
5 namespace Friendica\Util;
6
7 use Friendica\Core\Config;
8 use Friendica\Core\Hook;
9 use Friendica\Core\Logger;
10 use Friendica\Protocol\Email;
11
12 /**
13  * class to handle emailing
14  */
15 class Emailer
16 {
17         /**
18          * Send a multipart/alternative message with Text and HTML versions
19          *
20          * @param array $params parameters
21          *                      fromName             name of the sender
22          *                      fromEmail            email of the sender
23          *                      replyTo              address to direct responses
24          *                      toEmail              destination email address
25          *                      messageSubject       subject of the message
26          *                      htmlVersion          html version of the message
27          *                      textVersion          text only version of the message
28          *                      additionalMailHeader additions to the SMTP mail header
29          *                      optional             uid user id of the destination user
30          *
31          * @return bool
32          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
33          */
34         public static function send(array $params)
35         {
36                 $params['sent'] = false;
37
38                 Hook::callAll('emailer_send_prepare', $params);
39
40                 if ($params['sent']) {
41                         return true;
42                 }
43
44                 $email_textonly = false;
45                 if (!empty($params['uid'])) {
46                         $email_textonly = DI::pConfig()->get($params['uid'], "system", "email_textonly");
47                 }
48
49                 $fromName = Email::encodeHeader(html_entity_decode($params['fromName'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
50                 $messageSubject = Email::encodeHeader(html_entity_decode($params['messageSubject'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
51
52                 // generate a mime boundary
53                 $mimeBoundary   =rand(0, 9)."-"
54                                 .rand(100000000, 999999999)."-"
55                                 .rand(100000000, 999999999)."=:"
56                                 .rand(10000, 99999);
57
58                 // generate a multipart/alternative message header
59                 $messageHeader = ($params['additionalMailHeader'] ?? '') .
60                                                 "From: $fromName <{$params['fromEmail']}>\n" .
61                                                 "Reply-To: $fromName <{$params['replyTo']}>\n" .
62                                                 "MIME-Version: 1.0\n" .
63                                                 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
64
65                 // assemble the final multipart message body with the text and html types included
66                 $textBody       =       chunk_split(base64_encode($params['textVersion']));
67                 $htmlBody       =       chunk_split(base64_encode($params['htmlVersion']));
68                 $multipartMessageBody = "--" . $mimeBoundary . "\n" .                                   // plain text section
69                                                                 "Content-Type: text/plain; charset=UTF-8\n" .
70                                                                 "Content-Transfer-Encoding: base64\n\n" .
71                                                                 $textBody . "\n";
72
73                 if (!$email_textonly && !is_null($params['htmlVersion'])) {
74                         $multipartMessageBody .=
75                                 "--" . $mimeBoundary . "\n" .                           // text/html section
76                                 "Content-Type: text/html; charset=UTF-8\n" .
77                                 "Content-Transfer-Encoding: base64\n\n" .
78                                 $htmlBody . "\n";
79                 }
80                 $multipartMessageBody .=
81                         "--" . $mimeBoundary . "--\n";                                  // message ending
82
83                 if (Config::get("system", "sendmail_params", true)) {
84                         $sendmail_params = '-f ' . $params['fromEmail'];
85                 } else {
86                         $sendmail_params = null;
87                 }
88
89                 // send the message
90                 $hookdata = [
91                         'to' => $params['toEmail'],
92                         'subject' => $messageSubject,
93                         'body' => $multipartMessageBody,
94                         'headers' => $messageHeader,
95                         'parameters' => $sendmail_params,
96                         'sent' => false,
97                 ];
98
99                 Hook::callAll("emailer_send", $hookdata);
100
101                 if ($hookdata['sent']) {
102                         return true;
103                 }
104
105                 $res = mail(
106                         $hookdata['to'],
107                         $hookdata['subject'],
108                         $hookdata['body'],
109                         $hookdata['headers'],
110                         $hookdata['parameters']
111                 );
112                 Logger::log("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, Logger::DEBUG);
113                 Logger::log("return value " . (($res)?"true":"false"), Logger::DEBUG);
114                 return $res;
115         }
116 }