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