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