]> git.mxchange.org Git - friendica.git/blob - src/Util/Emailer.php
Merge pull request #7366 from guzzisti/install-md-wrapup
[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(array $params)
36         {
37                 $params['sent'] = false;
38
39                 Hook::callAll('emailer_send_prepare', $params);
40
41                 if ($params['sent']) {
42                         return true;
43                 }
44
45                 $email_textonly = false;
46                 if (!empty($params['uid'])) {
47                         $email_textonly = PConfig::get($params['uid'], "system", "email_textonly");
48                 }
49
50                 $fromName = Email::encodeHeader(html_entity_decode($params['fromName'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
51                 $messageSubject = Email::encodeHeader(html_entity_decode($params['messageSubject'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
52
53                 // generate a mime boundary
54                 $mimeBoundary   =rand(0, 9)."-"
55                                 .rand(100000000, 999999999)."-"
56                                 .rand(100000000, 999999999)."=:"
57                                 .rand(10000, 99999);
58
59                 // generate a multipart/alternative message header
60                 $messageHeader = defaults($params, 'additionalMailHeader', '') .
61                                                 "From: $fromName <{$params['fromEmail']}>\n" .
62                                                 "Reply-To: $fromName <{$params['replyTo']}>\n" .
63                                                 "MIME-Version: 1.0\n" .
64                                                 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
65
66                 // assemble the final multipart message body with the text and html types included
67                 $textBody       =       chunk_split(base64_encode($params['textVersion']));
68                 $htmlBody       =       chunk_split(base64_encode($params['htmlVersion']));
69                 $multipartMessageBody = "--" . $mimeBoundary . "\n" .                                   // plain text section
70                                                                 "Content-Type: text/plain; charset=UTF-8\n" .
71                                                                 "Content-Transfer-Encoding: base64\n\n" .
72                                                                 $textBody . "\n";
73
74                 if (!$email_textonly && !is_null($params['htmlVersion'])) {
75                         $multipartMessageBody .=
76                                 "--" . $mimeBoundary . "\n" .                           // text/html section
77                                 "Content-Type: text/html; charset=UTF-8\n" .
78                                 "Content-Transfer-Encoding: base64\n\n" .
79                                 $htmlBody . "\n";
80                 }
81                 $multipartMessageBody .=
82                         "--" . $mimeBoundary . "--\n";                                  // message ending
83
84                 if (Config::get("system", "sendmail_params", true)) {
85                         $sendmail_params = '-f ' . $params['fromEmail'];
86                 } else {
87                         $sendmail_params = null;
88                 }
89
90                 // send the message
91                 $hookdata = [
92                         'to' => $params['toEmail'],
93                         'subject' => $messageSubject,
94                         'body' => $multipartMessageBody,
95                         'headers' => $messageHeader,
96                         'parameters' => $sendmail_params,
97                         'sent' => false,
98                 ];
99
100                 Hook::callAll("emailer_send", $hookdata);
101
102                 if ($hookdata['sent']) {
103                         return true;
104                 }
105
106                 $res = mail(
107                         $hookdata['to'],
108                         $hookdata['subject'],
109                         $hookdata['body'],
110                         $hookdata['headers'],
111                         $hookdata['parameters']
112                 );
113                 Logger::log("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, Logger::DEBUG);
114                 Logger::log("return value " . (($res)?"true":"false"), Logger::DEBUG);
115                 return $res;
116         }
117 }