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