]> git.mxchange.org Git - friendica.git/blob - include/Emailer.php
Adds 'emailer_send_prepare' and 'emailer_send' hooks
[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          */
18         static public function send($params) {
19
20                 call_hooks('emailer_send_prepare', $params);
21
22                 $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8');
23                 $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8');
24
25                 // generate a mime boundary
26                 $mimeBoundary   =rand(0,9)."-"
27                                 .rand(10000000000,9999999999)."-"
28                                 .rand(10000000000,9999999999)."=:"
29                                 .rand(10000,99999);
30
31                 // generate a multipart/alternative message header
32                 $messageHeader =
33                         $params['additionalMailHeader'] .
34                         "From: $fromName <{$params['fromEmail']}>\n" .
35                         "Reply-To: $fromName <{$params['replyTo']}>\n" .
36                         "MIME-Version: 1.0\n" .
37                         "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
38
39                 // assemble the final multipart message body with the text and html types included
40                 $textBody       =       chunk_split(base64_encode($params['textVersion']));
41                 $htmlBody       =       chunk_split(base64_encode($params['htmlVersion']));
42                 $multipartMessageBody =
43                         "--" . $mimeBoundary . "\n" .                                   // plain text section
44                         "Content-Type: text/plain; charset=UTF-8\n" .
45                         "Content-Transfer-Encoding: base64\n\n" .
46                         $textBody . "\n" .
47                         "--" . $mimeBoundary . "\n" .                                   // text/html section
48                         "Content-Type: text/html; charset=UTF-8\n" .
49                         "Content-Transfer-Encoding: base64\n\n" .
50                         $htmlBody . "\n" .
51                         "--" . $mimeBoundary . "--\n";                                  // message ending
52
53                 // send the message
54                 $hookdata = array(
55                         'to' => $params['toEmail'],
56                         'subject' => $messageSubject,
57                         'body' => $multipartMessageBody,
58                         'headers' => $messageHeader
59                 );
60                 call_hooks("emailer_send", $hookdata);
61                 $res = mail(
62                         $hookdata['to'],                                                        // send to address
63                         $hookdata['subject'],                                           // subject
64                         $hookdata['body'],                                                      // message body
65                         $hookdata['headers'],                                           // message headers
66                 );
67                 logger("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, LOGGER_DEBUG);
68                 logger("return value " . (($res)?"true":"false"), LOGGER_DEBUG);
69                 return $res;
70         }
71 }
72 ?>