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 =
if (isset($contents['attachments'])) {
$attachments = $contents['attachments'];
}
$att1 = isset($attachments[0]) ? $attachments[0] : '';
$att2 = isset($attachments[1]) ? $attachments[1] : '';
$att3 = isset($attachments[2]) ? $attachments[2] : '';
$attachments = array_map(
function ($attachmentPath) {
return realpath(__DIR__.'/../../../uploads/notifications/'.$attachmentPath);
},
$attachments
);
$sentSuccessfully = false;
$sentTime = '';
if (CBV\system_can_send_emails()) {
$response = \wpMandrill::mail($to_email, $subject, !empty($html) ? $html : $alttext, [], $attachments);
if (isset($response['status']) && !in_array($response['status'], ['rejected', 'invalid'])) {
$sentSuccessfully = true;
$sentTime = current_time('mysql', 1);
}
}
$wpdb->insert(
'wp_mail_daemon',
[
......@@ -274,8 +290,9 @@ function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args =
'attachment1' => $att1,
'attachment2' => $att2,
'attachment3' => $att3,
'sent' => 0,
'sent_date' => ''
'sent' => (int)$sentSuccessfully,
'sent_date' => $sentTime,
'created' => current_time('mysql', 1)
],
[
'%d',
......@@ -291,75 +308,11 @@ function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args =
'%s'
]
);
//send_email($uid,$email,$args, true);
}
}
// if the system notification has set current user than get current user otherwise loop through the users needed.
}
function send_email($uid = 0, $contents, $args, $override = false)
{
if ($uid == 0 && !isset($args['email'])) {
return;
}
if (isset($args['email'])) {
$to_email = $args['email'];
} else {
$user = new WP_User($uid);
$email_address_preference = get_user_meta($user->ID, 'email_address_preference', true);
if (empty($email_address_preference)) {
$to_email = $user->user_email;
} else {
$pp = strtolower($email_address_preference)."_";
$to_email = get_user_meta($user->ID, $pp.'email', true);
if (empty($to_email)) {
$to_email = $user->user_email;
}
}
}
$from_address = get_bloginfo('admin_email');
$subject = strip_tags($contents['subject']);
$html = $contents['html'];
$alttext = $contents['text'];
$headers = "";
if (empty($html)) {
$message = $alttext;
$message = strip_tags($message);
// it's text based only... no need to check format preference.
} else {
$message = $html;
$headers .= 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
// it's HTML formatted.
// Check user preference if they want Text ONly??
}
foreach ($args as $key => $val) {
if ((filter_var($val, FILTER_VALIDATE_URL) !== false) && !empty($html)) {
$message = str_replace("{".$key."}", "<a href='".$val."'>".$val."</a>", $message);
} else {
$message = str_replace("{".$key."}", $val, $message);
$subject = str_replace("{".$key."}", $val, $subject);
}
}
// Additional headers
//$headers .= 'To: '.$to_email.' <'.$to_email.'>' . "\r\n";
$headers .= 'From: CICBV <'.$from_address.'>'."\r\n";
//die("To: $to_email, Subject: $subject, Message: $message, Header: $headers");
mail($to_email, $subject, $message, $headers);
}
function getGroups($grpID = 0)
{
global $userAccessManager;
......