9fe33ab5 by Marty Penner

Use Mandrill for sending email

Also send it immediately when a notification is triggered. If it fails, mark "sent" as false in the db so the cron can pick it up later.
1 parent 66546631
...@@ -257,11 +257,27 @@ function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args = ...@@ -257,11 +257,27 @@ function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args =
257 if (isset($contents['attachments'])) { 257 if (isset($contents['attachments'])) {
258 $attachments = $contents['attachments']; 258 $attachments = $contents['attachments'];
259 } 259 }
260
261 $att1 = isset($attachments[0]) ? $attachments[0] : ''; 260 $att1 = isset($attachments[0]) ? $attachments[0] : '';
262 $att2 = isset($attachments[1]) ? $attachments[1] : ''; 261 $att2 = isset($attachments[1]) ? $attachments[1] : '';
263 $att3 = isset($attachments[2]) ? $attachments[2] : ''; 262 $att3 = isset($attachments[2]) ? $attachments[2] : '';
264 263
264 $attachments = array_map(
265 function ($attachmentPath) {
266 return realpath(__DIR__.'/../../../uploads/notifications/'.$attachmentPath);
267 },
268 $attachments
269 );
270
271 $sentSuccessfully = false;
272 $sentTime = '';
273 if (CBV\system_can_send_emails()) {
274 $response = \wpMandrill::mail($to_email, $subject, !empty($html) ? $html : $alttext, [], $attachments);
275 if (isset($response['status']) && !in_array($response['status'], ['rejected', 'invalid'])) {
276 $sentSuccessfully = true;
277 $sentTime = current_time('mysql', 1);
278 }
279 }
280
265 $wpdb->insert( 281 $wpdb->insert(
266 'wp_mail_daemon', 282 'wp_mail_daemon',
267 [ 283 [
...@@ -274,8 +290,9 @@ function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args = ...@@ -274,8 +290,9 @@ function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args =
274 'attachment1' => $att1, 290 'attachment1' => $att1,
275 'attachment2' => $att2, 291 'attachment2' => $att2,
276 'attachment3' => $att3, 292 'attachment3' => $att3,
277 'sent' => 0, 293 'sent' => (int)$sentSuccessfully,
278 'sent_date' => '' 294 'sent_date' => $sentTime,
295 'created' => current_time('mysql', 1)
279 ], 296 ],
280 [ 297 [
281 '%d', 298 '%d',
...@@ -291,75 +308,11 @@ function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args = ...@@ -291,75 +308,11 @@ function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args =
291 '%s' 308 '%s'
292 ] 309 ]
293 ); 310 );
294 //send_email($uid,$email,$args, true);
295 } 311 }
296 } 312 }
297 // if the system notification has set current user than get current user otherwise loop through the users needed. 313 // if the system notification has set current user than get current user otherwise loop through the users needed.
298 } 314 }
299 315
300 function send_email($uid = 0, $contents, $args, $override = false)
301 {
302 if ($uid == 0 && !isset($args['email'])) {
303 return;
304 }
305
306 if (isset($args['email'])) {
307 $to_email = $args['email'];
308 } else {
309 $user = new WP_User($uid);
310
311 $email_address_preference = get_user_meta($user->ID, 'email_address_preference', true);
312 if (empty($email_address_preference)) {
313 $to_email = $user->user_email;
314 } else {
315
316 $pp = strtolower($email_address_preference)."_";
317 $to_email = get_user_meta($user->ID, $pp.'email', true);
318
319 if (empty($to_email)) {
320 $to_email = $user->user_email;
321 }
322 }
323 }
324
325 $from_address = get_bloginfo('admin_email');
326
327 $subject = strip_tags($contents['subject']);
328 $html = $contents['html'];
329 $alttext = $contents['text'];
330
331 $headers = "";
332
333 if (empty($html)) {
334 $message = $alttext;
335 $message = strip_tags($message);
336 // it's text based only... no need to check format preference.
337 } else {
338 $message = $html;
339 $headers .= 'MIME-Version: 1.0'."\r\n";
340 $headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
341 // it's HTML formatted.
342 // Check user preference if they want Text ONly??
343 }
344
345 foreach ($args as $key => $val) {
346 if ((filter_var($val, FILTER_VALIDATE_URL) !== false) && !empty($html)) {
347 $message = str_replace("{".$key."}", "<a href='".$val."'>".$val."</a>", $message);
348 } else {
349 $message = str_replace("{".$key."}", $val, $message);
350 $subject = str_replace("{".$key."}", $val, $subject);
351 }
352 }
353
354 // Additional headers
355 //$headers .= 'To: '.$to_email.' <'.$to_email.'>' . "\r\n";
356 $headers .= 'From: CICBV <'.$from_address.'>'."\r\n";
357
358 //die("To: $to_email, Subject: $subject, Message: $message, Header: $headers");
359
360 mail($to_email, $subject, $message, $headers);
361 }
362
363 function getGroups($grpID = 0) 316 function getGroups($grpID = 0)
364 { 317 {
365 global $userAccessManager; 318 global $userAccessManager;
......