]> git.mxchange.org Git - friendica.git/blob - include/Emailer.php
fix account_type
[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                 $email_textonly = False;
24                 if (x($params,"uid")) {
25                         $email_textonly = get_pconfig($params['uid'], "system", "email_textonly");
26                 }
27
28                 $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8');
29                 $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8');
30
31                 // generate a mime boundary
32                 $mimeBoundary   =rand(0,9)."-"
33                                 .rand(10000000000,99999999999)."-"
34                                 .rand(10000000000,99999999999)."=:"
35                                 .rand(10000,99999);
36
37                 // generate a multipart/alternative message header
38                 $messageHeader =
39                         $params['additionalMailHeader'] .
40                         "From: $fromName <{$params['fromEmail']}>\n" .
41                         "Reply-To: $fromName <{$params['replyTo']}>\n" .
42                         "MIME-Version: 1.0\n" .
43                         "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
44
45                 // assemble the final multipart message body with the text and html types included
46                 $textBody       =       chunk_split(base64_encode($params['textVersion']));
47                 $htmlBody       =       chunk_split(base64_encode($params['htmlVersion']));
48                 $multipartMessageBody =
49                         "--" . $mimeBoundary . "\n" .                                   // plain text section
50                         "Content-Type: text/plain; charset=UTF-8\n" .
51                         "Content-Transfer-Encoding: base64\n\n" .
52                         $textBody . "\n";
53
54                 if (!$email_textonly && !is_null($params['htmlVersion'])){
55                         $multipartMessageBody .=
56                                 "--" . $mimeBoundary . "\n" .                           // text/html section
57                                 "Content-Type: text/html; charset=UTF-8\n" .
58                                 "Content-Transfer-Encoding: base64\n\n" .
59                                 $htmlBody . "\n";
60                 }
61                 $multipartMessageBody .=
62                         "--" . $mimeBoundary . "--\n";                                  // message ending
63
64                 // send the message
65                 $hookdata = array(
66                         'to' => $params['toEmail'],
67                         'subject' => $messageSubject,
68                         'body' => $multipartMessageBody,
69                         'headers' => $messageHeader
70                 );
71                 //echo "<pre>"; var_dump($hookdata); killme();
72                 call_hooks("emailer_send", $hookdata);
73                 $res = mail(
74                         $hookdata['to'],                                                        // send to address
75                         $hookdata['subject'],                                           // subject
76                         $hookdata['body'],                                                      // message body
77                         $hookdata['headers']                                            // message headers
78                 );
79                 logger("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, LOGGER_DEBUG);
80                 logger("return value " . (($res)?"true":"false"), LOGGER_DEBUG);
81                 return $res;
82         }
83 }
84 ?>