]> git.mxchange.org Git - friendica.git/blob - include/enotify.php
new notification module working for "email message received"
[friendica.git] / include / enotify.php
1 <?php
2
3
4                 // send email notification if requested.
5 /*
6                 $notif_params = array(
7                         'type' => NOTIFY_MAIL,
8                         'notify_flags' => $importer['notify-flags'],
9                         'language' => $importer['language'],
10                         'to_name' => $importer['username'],
11                         'to_email' => $importer['email'],
12                         'item' => $msg,
13                         'source_name' => $msg['from-name'],
14                         'source_link' => $importer['url'],
15                         'source_photo' => $importer['thumb'],
16                 );
17 */                      
18                 //notification($notif_params);
19
20
21 function notification($params) {
22
23         logger('notification: entry');
24
25         $a = get_app();
26         $banner = t('Friendica Notification');
27         $product = FRIENDICA_PLATFORM;
28         $siteurl = z_path();
29         $thanks = t('Thank You,');
30         $sitename = get_config('config','sitename');
31         $site_admin = sprintf( t('%s Administrator'), $sitename);
32
33         $sender_name = t('Administrator');
34         $sender_email = t('noreply') . '@' . $a->get_hostname();
35
36         $title = $params['item']['title'];
37         $body = $params['item']['body'];
38
39         if($params['type'] == NOTIFY_MAIL) {
40
41                 logger('notification: email');
42
43                 $subject =      sprintf( t('New mail received at %s'),$sitename);
44
45                 $preamble = sprintf( t('%s sent you a new private message at %s.'),$params['source_name'],$sitename);
46
47                 $sitelink = t('Please visit %s to view and/or reply to your private messages.');
48                 $tsitelink = sprintf( $sitelink, $siteurl . '/message' );
49                 $hsitelink = sprintf( $sitelink, '<a href="' . $siteurl . '/message">' . $sitename . '</a>');
50         }
51
52
53         // send email notification if notification preferences permit
54
55         require_once('bbcode.php');
56         if(intval($params['notify_flags']) & intval($params['type'])) {
57
58                 logger('notification: sending email');
59
60                 push_lang($params['language']);
61
62                 $textversion = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n",
63                         $body))),ENT_QUOTES,'UTF-8'));
64                 $htmlversion = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), 
65                         "<br />\n",$body))));
66
67                 // load the template for private message notifications
68                 $tpl = get_markup_template('email_notify_html.tpl');
69                 $email_html_body = replace_macros($tpl,array(
70                         '$banner'       => $banner,
71                         '$product'      => $product,
72                         '$preamble'     => $preamble,
73                         '$sitename'     => $sitename,
74                         '$siteurl'      => $siteurl,
75                         '$source_name'  => $parama['source_name'],
76                         '$source_link'  => $params['source_link'],
77                         '$source_photo' => $params['source_photo'],
78                         '$username'     => $params['to_name'],
79                         '$hsitelink'    => $hsitelink,
80                         '$thanks'       => $thanks,
81                         '$site_admin'   => $site_admin,
82                         '$title'                => stripslashes($title),
83                         '$htmlversion'  => $htmlversion,        
84                 ));
85                 
86                 // load the template for private message notifications
87                 $tpl = get_markup_template('email_notify_text.tpl');
88                 $email_text_body = replace_macros($tpl,array(
89                         '$banner'       => $banner,
90                         '$product'      => $product,
91                         '$preamble'     => $preamble,
92                         '$sitename'     => $sitename,
93                         '$siteurl'      => $siteurl,
94                         '$source_name'  => $parama['source_name'],
95                         '$source_link'  => $params['source_link'],
96                         '$source_photo' => $params['source_photo'],
97                         '$username'     => $params['to_name'],
98                         '$tsitelink'    => $tsitelink,
99                         '$thanks'       => $thanks,
100                         '$site_admin'   => $site_admin,
101                         '$title'                => stripslashes($title),
102                         '$textversion'  => $textversion,        
103                 ));
104
105                 logger('text: ' . $email_text_body);
106
107                 // use the EmailNotification library to send the message
108
109                 enotify::send(array(
110                         'fromName' => $sender_name,
111                         'fromEmail' => $sender_email,
112                         'replyTo' => $sender_email,
113                         'toEmail' => $params['to_email'],
114                         'messageSubject' => $subject,
115                         'htmlVersion' => $email_html_body,
116                         'textVersion' => $email_text_body
117                 ));
118                 pop_lang();
119         }
120 }
121
122 require_once('include/email.php');
123
124 class enotify {
125         /**
126          * Send a multipart/alternative message with Text and HTML versions
127          *
128          * @param fromName                      name of the sender
129          * @param fromEmail                     email fo the sender
130          * @param replyTo                       replyTo address to direct responses
131          * @param toEmail                       destination email address
132          * @param messageSubject        subject of the message
133          * @param htmlVersion           html version of the message
134          * @param textVersion           text only version of the message
135          */
136         static public function send($params) {
137
138                 logger('enotify: send: ' . print_r($params,true));
139                 $fromName = email_header_encode($params['fromName'],'UTF-8'); 
140                 $messageSubject = email_header_encode($params['messageSubject'],'UTF-8');
141                 
142                 
143                 // generate a mime boundary
144                 $mimeBoundary   =rand(0,9)."-"
145                                 .rand(10000000000,9999999999)."-"
146                                 .rand(10000000000,9999999999)."=:"
147                                 .rand(10000,99999);
148
149                 // generate a multipart/alternative message header
150                 $messageHeader =
151                         "From: {$params['fromName']} <{$params['fromEmail']}>\n" . 
152                         "Reply-To: {$params['replyTo']}\n" .
153                         "MIME-Version: 1.0\n" .
154                         "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
155
156                 // assemble the final multipart message body with the text and html types included
157                 $textBody       =       chunk_split(base64_encode($params['textVersion']));
158                 $htmlBody       =       chunk_split(base64_encode($params['htmlVersion']));
159                 $multipartMessageBody =
160                         "--" . $mimeBoundary . "\n" .                                   // plain text section
161                         "Content-Type: text/plain; charset=UTF-8\n" .
162                         "Content-Transfer-Encoding: base64\n\n" .
163                         $textBody . "\n" .
164                         "--" . $mimeBoundary . "\n" .                                   // text/html section
165                         "Content-Type: text/html; charset=UTF-8\n" .
166                         "Content-Transfer-Encoding: base64\n\n" .
167                         $htmlBody . "\n" .
168                         "--" . $mimeBoundary . "--\n";                                  // message ending
169
170                 // send the message
171                 $res = mail(
172                         $params['toEmail'],                                                                             // send to address
173                         $params['messageSubject'],                                                              // subject
174                         $multipartMessageBody,                                                  // message body
175                         $messageHeader                                                                  // message headers
176                 );
177                 logger("sendTextHtmlEmail: returns " . $res, LOGGER_DEBUG);
178         }
179 }
180 ?>