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