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