]> git.mxchange.org Git - friendica.git/blob - src/Util/Emailer.php
Avoid "Failed to parse time string (-001-11-30T00:00:00+00:00 + 32 days)"
[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\PConfig;
9 use Friendica\Protocol\Email;
10
11 /**
12  * @brief class to handle emailing
13  */
14 class Emailer
15 {
16         /**
17          * Send a multipart/alternative message with Text and HTML versions
18          *
19          * @param array $params parameters
20          *                      fromName name of the sender
21          *                      fromEmail                        email fo the sender
22          *                      replyTo                      replyTo address to direct responses
23          *                      toEmail                      destination email address
24          *                      messageSubject       subject of the message
25          *                      htmlVersion                  html version of the message
26          *                      textVersion                  text only version of the message
27          *                      additionalMailHeader additions to the smtp mail header
28          *                      optional             uid user id of the destination user
29          *
30          * @return object
31          */
32         public static function send($params)
33         {
34                 Addon::callHooks('emailer_send_prepare', $params);
35
36                 $email_textonly = false;
37                 if (x($params, "uid")) {
38                         $email_textonly = PConfig::get($params['uid'], "system", "email_textonly");
39                 }
40
41                 $fromName = Email::encodeHeader(html_entity_decode($params['fromName'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
42                 $messageSubject = Email::encodeHeader(html_entity_decode($params['messageSubject'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
43
44                 // generate a mime boundary
45                 $mimeBoundary   =rand(0, 9)."-"
46                                 .rand(100000000, 999999999)."-"
47                                 .rand(100000000, 999999999)."=:"
48                                 .rand(10000, 99999);
49
50                 // generate a multipart/alternative message header
51                 $messageHeader = $params['additionalMailHeader'] .
52                                                 "From: $fromName <{$params['fromEmail']}>\n" .
53                                                 "Reply-To: $fromName <{$params['replyTo']}>\n" .
54                                                 "MIME-Version: 1.0\n" .
55                                                 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
56
57                 // assemble the final multipart message body with the text and html types included
58                 $textBody       =       chunk_split(base64_encode($params['textVersion']));
59                 $htmlBody       =       chunk_split(base64_encode($params['htmlVersion']));
60                 $multipartMessageBody = "--" . $mimeBoundary . "\n" .                                   // plain text section
61                                                                 "Content-Type: text/plain; charset=UTF-8\n" .
62                                                                 "Content-Transfer-Encoding: base64\n\n" .
63                                                                 $textBody . "\n";
64
65                 if (!$email_textonly && !is_null($params['htmlVersion'])) {
66                         $multipartMessageBody .=
67                                 "--" . $mimeBoundary . "\n" .                           // text/html section
68                                 "Content-Type: text/html; charset=UTF-8\n" .
69                                 "Content-Transfer-Encoding: base64\n\n" .
70                                 $htmlBody . "\n";
71                 }
72                 $multipartMessageBody .=
73                         "--" . $mimeBoundary . "--\n";                                  // message ending
74
75                 // send the message
76                 $hookdata = [
77                         'to' => $params['toEmail'],
78                         'subject' => $messageSubject,
79                         'body' => $multipartMessageBody,
80                         'headers' => $messageHeader
81                 ];
82                 //echo "<pre>"; var_dump($hookdata); killme();
83                 Addon::callHooks("emailer_send", $hookdata);
84                 $res = mail(
85                         $hookdata['to'],                                                        // send to address
86                         $hookdata['subject'],                                           // subject
87                         $hookdata['body'],                                                      // message body
88                         $hookdata['headers']                                            // message headers
89                 );
90                 logger("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, LOGGER_DEBUG);
91                 logger("return value " . (($res)?"true":"false"), LOGGER_DEBUG);
92                 return $res;
93         }
94 }