You are here

function pm_email_notify_privatemsg_message_insert in Privatemsg 7

Same name and namespace in other branches
  1. 6.2 pm_email_notify/pm_email_notify.module \pm_email_notify_privatemsg_message_insert()
  2. 6 pm_email_notify/pm_email_notify.module \pm_email_notify_privatemsg_message_insert()
  3. 7.2 pm_email_notify/pm_email_notify.module \pm_email_notify_privatemsg_message_insert()

Implements hook_privatemsg_message_insert().

File

pm_email_notify/pm_email_notify.module, line 33
Notifies users about new Private Messages via Email.

Code

function pm_email_notify_privatemsg_message_insert($message) {
  foreach ($message->recipients as $recipient) {

    // check if recipient enabled email notifications
    if (isset($recipient->uid) && _pm_email_notify_is_enabled($recipient->uid)) {

      // send them a new pm notification email if they did
      $params['recipient'] = $recipient;
      $params['message'] = $message;

      // token replace for email from address
      $data = array(
        'privatemsg_message' => $params['message'],
        'privatemsg_recipient' => $params['recipient'],
      );
      $options = array(
        'language' => user_preferred_language($params['recipient']),
        // Don't sanitize output since this is used in an email, not a browser.
        'sanitize' => FALSE,
        // Custom token to avoid custom token handling.
        'privatemsg-display-invalid' => FALSE,
      );
      $from = trim(token_replace(variable_get('pm_email_notify_from', ''), $data, $options));
      drupal_mail('pm_email_notify', 'notice', $recipient->mail, user_preferred_language($recipient), $params, !empty($from) ? $from : NULL);
    }
  }
}