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