--- privatemsg.module 2008-03-20 17:39:48.000000000 +0800 +++ privatemsg.module 2008-03-20 17:45:37.000000000 +0800 @@ -5,6 +5,10 @@ define('PRIVATEMSG_FOLDER_RECYCLE_BIN', define('PRIVATEMSG_FOLDER_INBOX', 0); define('PRIVATEMSG_FOLDER_SENT', 1); +define('PRIVATEMSG_MAIL_NONE', 0); +define('PRIVATEMSG_MAIL_DIGEST', 1); +define('PRIVATEMSG_MAIL_INDIVIDUAL', 2); + /** * Implementation of hook_help(). */ @@ -270,6 +274,7 @@ function privatemsg_cron() { // Perform these actions just once per day. if (variable_get('privatemsg_last_cron', 0) < (time() - 3600*24)) { _privatemsg_prune(); + _privatemsg_mailalert(); variable_set('privatemsg_last_cron', time()); } } @@ -374,6 +379,13 @@ function privatemsg_user($type, &$edit, '#default_value' => isset($edit['privatemsg_setmessage_notify']) ? $edit['privatemsg_setmessage_notify'] : 1, '#description' => t('Show status message on every page until new messages are read.') ); + $form['privatemsg_settings']['privatemsg_mailnotify'] = array( + '#type' => 'radios', + '#title' => t("Notify of new private message by email"), + '#options' => array(PRIVATEMSG_MAIL_NONE => t('Never'), PRIVATEMSG_MAIL_DIGEST => t('Daily'), PRIVATEMSG_MAIL_INDIVIDUAL => t('Every message')), + '#default_value' => isset($edit['privatemsg_mailnotify']) ? $edit['privatemsg_mailnotify'] : PRIVATEMSG_MAIL_INDIVIDUAL, + '#description' => t("Select if you would like to be notified by email of new private messages."), + ); return $form; } break; @@ -651,6 +663,70 @@ function privatemsg_configure() { '#default_value' => variable_get('privatemsg_default_setmessage_notify', 1), '#description' => t("Check this box to set the default value of the 'Aggressive notification of new messages' user setting."), ); + $form['admin_default']['privatemsg_default_mailnotify'] = array( + '#type' => 'radios', + '#title' => t("Notify of new private message by email"), + '#options' => array(PRIVATEMSG_MAIL_NONE => t('Never'), PRIVATEMSG_MAIL_DIGEST => t('Daily'), PRIVATEMSG_MAIL_INDIVIDUAL => t('Every message')), + '#default_value' => variable_get('privatemsg_default_mailnotify', PRIVATEMSG_MAIL_INDIVIDUAL), + '#description' => t("Check this box to set the default value of the 'Notify of new privatemessage by email' user setting."), + ); + $form['admin_default']['privatemsg_mailnotify_subject'] = array( + '#type' => 'textfield', + '#title' => t('Subject of new private message email notifications'), + '#default_value' => variable_get('privatemsg_mailnotify_subject', t('You have a new private message')), + '#size' => 50, + '#maxlength' => 64, + ); + $form['admin_default']['privatemsg_mailnotify_message'] = array( + '#type' => 'textarea', + '#title' => t('Body of new private message email notifications'), + '#default_value' => variable_get('privatemsg_mailnotify_message', " +Hi !name, + +You have received a new private message on !site from !sender titled + +\"!title\" + +To read your message, follow this link: +!read + +If you don't want to receive these emails again, change your preferences here: +!preferences + +-- +!site +"), + '#cols' => 80, + '#rows' => 10, + '#description' => t('Set the email message that a user will receive if they receive email alerts for private messages at the time the private message is received.'), + ); + $form['admin_default']['privatemsg_mailnotify_digest_subject'] = array( + '#type' => 'textfield', + '#title' => t('Subject of new private message email digest notifications'), + '#default_value' => variable_get('privatemsg_mailnotify_digest_subject', t('You have new private messages')), + '#size' => 50, + '#maxlength' => 64, + ); + $form['admin_default']['privatemsg_mailnotify_digest_message'] = array( + '#type' => 'textarea', + '#title' => t('Body of new private message email digest notifications'), + '#default_value' => variable_get('privatemsg_mailnotify_digest_message', " +Hi !name, + +You have received new private messages on !site +To read your messages, follow this link: +!read + +If you don't want to receive these emails again, change your preferences here: +!preferences + +-- +!site +"), + '#cols' => 80, + '#rows' => 10, + '#description' => t('Set the email message that a user will receive if they receive email alerts for private messages in "Digest" mode.'), + ); return system_settings_form($form); } @@ -1648,6 +1724,17 @@ function _privatemsg_send($sender, $reci $variables[str_replace('!', '!original_', $name)] = $value; } $result = db_query("INSERT INTO {privatemsg} (id, author, recipient, subject, message, timestamp, newmsg, hostname, format, thread, type, variables) VALUES (%d, %d, %d, '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s')", $message_id, $sender->uid, $recipient->uid, $subject, $body, time(), 1, getenv('REMOTE_ADDR'), $format, $thread, $type, serialize($variables)); + $default_mailnotify = variable_get('privatemsg_default_mailnotify', PRIVATEMSG_MAIL_INDIVIDUAL); + + if ($result !== false + && isset($recipient->privatemsg_mailnotify) + && $recipient->privatemsg_mailnotify == PRIVATEMSG_MAIL_INDIVIDUAL) { + _privatemsg_sendmail(array('recipient' => $recipient, 'sender' => $sender, 'subject' => $subject, 'id' => $message_id), 1); + } else if ($result !== false + && (!isset($recipient->privatemsg_mailnotify)) + && ($default_mailnotify == PRIVATEMSG_MAIL_INDIVIDUAL)) { + _privatemsg_sendmail(array('recipient' => $recipient, 'sender' => $sender, 'subject' => $subject, 'id' => $message_id), 1); + } if ($points = variable_get('privatemsg_userpoints', 0)) { module_invoke('userpoints', 'userpointsapi', 'points', $points, $sender->uid, 'privatemsg'); } @@ -1656,6 +1743,76 @@ function _privatemsg_send($sender, $reci return $result ? $message_id : $result; } +function _privatemsg_sendmail($message_details, $new_count) { + global $locale; + static $from, $languages; + + if (!isset($from)) { + $from = variable_get('site_mail', ini_get('sendmail_from')); + $languages = array(); + if (function_exists('locale')) { + $languages = locale_supported_languages(); + $languages = $languages['name']; + } + } + $save_locale = $locale; + + // use each user's individual locale + if (isset($languages[$message_details['recipient']->language])) { + $locale = $message_details['recipient']->language; + } + + $variables = array( + '!site' => variable_get('site_name', 'drupal'), + '!name' => $message_details['recipient']->name, + '!sender' => isset($message_details['sender']) ? check_plain($message_details['sender']->name) : '', + '!title' => isset($message_details['subject']) ? check_plain($message_details['subject']) : '', + '!read' => isset($message_details['id']) ? url('privatemsg/view/'. $message_details['id'], NULL, NULL, 1) : url('privatemsg', NULL, NULL, 1), + '!preferences' => url('user/'. $message_details['recipient']->uid .'/edit', NULL, NULL, 1), + ); + + $subject = strtr(format_plural($new_count, variable_get('privatemsg_mailnotify_subject', 'You have a new private message'), variable_get('privatemsg_mailnotify_digest_subject', 'You have new private messages')), $variables); + $message = strtr(format_plural($new_count, variable_get('privatemsg_mailnotify_message', +"Hi !name, + +You have received a new private message on !site from !sender titled + +\"!title\" + +To read your message, follow this link: +!read + +If you don't want to receive these emails again, change your preferences here: +!preferences + +-- +!site" +), variable_get('privatemsg_mailnotify_digest_message', +"Hi !name, + +You have received new private messages on !site +To read your messages, follow this link: +!read + +If you don't want to receive these emails again, change your preferences here: +!preferences + +-- +!site" +)), $variables); + + $headers = array( + 'From' => $from, + 'Reply-to' => $from, + 'Return-Path' => $from, + 'Errors-to' => $from, + ); + drupal_mail('privatemsg_mailalert', $message_details['recipient']->mail, $subject, $message, $from, $headers); + + // revert to previous (site default) locale + $locale = $save_locale; +} + /** * Return a version of type that can be used as an object property key * (change whitespace to _) @@ -2414,6 +2571,29 @@ function privatemsg_folder_access($uid, return FALSE; } +function _privatemsg_mailalert() { + $result = db_query('SELECT p.recipient, COUNT(*) AS message_count FROM {privatemsg} p INNER JOIN {users} u ON p.recipient = u.uid WHERE p.newmsg = 1 AND p.recipient_del = 0 AND u.status = 1 GROUP BY p.recipient'); + $default_mailnotify = variable_get('privatemsg_default_mailnotify', PRIVATEMSG_MAIL_INDIVIDUAL); + + while ($alert = db_fetch_object($result)) { + $user = user_load(array('uid' => $alert->recipient)); + + if ((isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1) + && isset($user->privatemsg_mailnotify) + && $user->privatemsg_mailnotify == PRIVATEMSG_MAIL_DIGEST + && $user->status != 0) { + _privatemsg_sendmail(array('recipient' => $user), $alert->message_count); + } + else if ((isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1) + && (!isset($user->privatemsg_mailnotify)) + && $default_mailnotify == PRIVATEMSG_MAIL_DIGEST + && $user->status != 0) { + // User didn't specify, but sitewide daily notification is on. + _privatemsg_sendmail(array('recipient' => $user), $alert->message_count); + } + } +} + function _privatemsg_prune() { // move deleted message older than 1 month to archive table, and optimize table $result = db_query('SELECT * FROM {privatemsg} WHERE author_del = 1 AND recipient_del = 1 AND timestamp < %d', time() - 3600*24*30); @@ -2473,5 +2653,6 @@ function _privatemsg_user_add_defaults(& if (!isset($account->privatemsg_allow)) { $account->privatemsg_allow = variable_get('privatemsg_default_allow', 1); $account->privatemsg_setmessage_notify = variable_get('privatemsg_default_setmessage_notify', 1); + $account->privatemsg_mailnotify = variable_get('privatemsg_default_mailnotify', PRIVATEMSG_MAIL_INDIVIDUAL); } }