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