Example #1
0
    /**
     * Send email notification to recipients on new thread or new message event.
     *
     * @param boolean true if new thread, false if new message in the current thread
     * @param boolean the User who sent the message, in case of current User it may be NULL ( This is not the current User e.g. in case of welcome messages )
     * @return boolean True if all messages could be sent, false otherwise.
     */
    function send_email_notifications($new_thread = true, $from_User = NULL)
    {
        global $DB, $current_User, $admin_url, $baseurl, $app_name;
        global $Settings, $UserSettings, $servertimenow;
        // Select recipients of the current thread:
        $SQL = new SQL();
        $SQL->SELECT('u.user_ID, us.uset_value as notify_messages');
        $SQL->FROM('T_messaging__threadstatus ts
						INNER JOIN T_messaging__contact c
							ON ts.tsta_user_ID = c.mct_to_user_ID AND c.mct_from_user_ID = ' . $this->author_user_ID . ' AND c.mct_blocked = 0
						INNER JOIN T_users u
							ON ts.tsta_user_ID = u.user_ID
						LEFT OUTER JOIN T_users__usersettings us ON u.user_ID = us.uset_user_ID AND us.uset_name = "notify_messages"');
        $SQL->WHERE('ts.tsta_thread_ID = ' . $this->Thread->ID . ' AND ts.tsta_user_ID <> ' . $this->author_user_ID);
        $thrd_recipients = $DB->get_assoc($SQL->get());
        // set message link:
        list($message_link, $prefs_link) = get_messages_link_to($this->thread_ID);
        // Construct message subject and body:
        if ($new_thread) {
            $subject = NT_('%s just sent you a new message!');
        } elseif (count($thrd_recipients) == 1) {
            $subject = NT_('%s just replied to your message!');
        } else {
            $subject = NT_('%s just replied to a conversation you are involved in!');
        }
        // Get other unread threads
        $other_unread_threads = get_users_unread_threads(array_keys($thrd_recipients), $this->thread_ID, 'array', 'html');
        // Load all users who will be notified
        $UserCache =& get_UserCache();
        $UserCache->load_list(array_keys($thrd_recipients));
        // Send email notifications.
        $ret = true;
        $def_notify_messages = $Settings->get('def_notify_messages');
        foreach ($thrd_recipients as $recipient_ID => $notify_messages) {
            // Send mail to recipients who needs to be notified. recipients are already loaded into the UserCache
            if (!($notify_messages || is_null($notify_messages) && $def_notify_messages)) {
                // User should NOT be notified
                continue;
            }
            $email_template_params = array('recipient_ID' => $recipient_ID, 'new_thread' => $new_thread, 'thrd_recipients' => $thrd_recipients, 'Message' => $this, 'message_link' => $message_link, 'other_unread_threads' => $other_unread_threads[$recipient_ID], 'from_User' => $from_User);
            $notify_User = $UserCache->get_by_ID($recipient_ID);
            // Change locale here to localize the email subject and content
            locale_temp_switch($notify_User->get('locale'));
            $sender_login = $from_User === NULL ? $current_User->login : $from_User->login;
            $localized_subject = sprintf(T_($subject), $sender_login);
            // Note: Not activated users won't get notification email
            if (send_mail_to_User($recipient_ID, $localized_subject, 'private_message_new', $email_template_params)) {
                // email sent successful, update las_unread_message_reminder timestamp, because the notification contains all unread messages
                $UserSettings->set('last_unread_messages_reminder', date2mysql($servertimenow), $recipient_ID);
            } else {
                // message was not sent
                $ret = false;
            }
            locale_restore_previous();
        }
        // update reminder timestamp changes
        $UserSettings->dbupdate();
        return $ret;
    }
        // Don't send a new reminder, because the user was not logged in since a very long time or we have already sent a reminder in the last x days ( x = delay value )
        unset($users_to_remind_ids[$index]);
    }
}
if (empty($users_to_remind_ids)) {
    // There is no user to remind after we have filtered out those ussers who haven't logged in since a long time
    $result_message = T_('It was not necessary to send any reminder!');
    return 1;
}
// Set TRUE to use gender settings from back office
global $is_admin_page;
$is_admin_page = true;
// Get all those user threads and their recipients where the corresponding users have unread messages
$unread_threads = get_users_unread_threads($users_to_remind_ids, NULL, 'array', 'html');
// Get unread thread urls
list($threads_link) = get_messages_link_to();
$reminder_sent = 0;
foreach ($users_to_remind_ids as $user_ID) {
    // send reminder email
    $email_template_params = array('unread_threads' => $unread_threads[$user_ID], 'threads_link' => $threads_link);
    $notify_User = $UserCache->get_by_ID($user_ID);
    // Change locale here to localize the email subject and content
    locale_temp_switch($notify_User->get('locale'));
    if (send_mail_to_User($user_ID, T_('You have unread messages!'), 'private_messages_unread_reminder', $email_template_params)) {
        // Update users last unread message reminder timestamp
        $UserSettings->set('last_unread_messages_reminder', date2mysql($servertimenow), $user_ID);
        // save UserSettings after each email, because the cron task mail fail and users won't be updated!
        $UserSettings->dbupdate();
        $reminder_sent++;
    }
    locale_restore_previous();