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