--- privatemsg.module.5.x-3.0	2009-01-29 11:01:56.000000000 -0500
+++ privatemsg.module	2009-01-30 11:24:10.000000000 -0500
@@ -1652,6 +1652,8 @@ function _privatemsg_send($sender, $reci
   }
   module_invoke('pm_subscriptions', 'subscriptions_handle', privatemsg_load($message_id));
 
+  module_invoke_all('privatemsg', 'privatemsg sent', privatemsg_load($message_id));
+
   return $result ? $message_id : $result;
 }
 
@@ -2529,4 +2531,152 @@ function theme_privatemsg_user_picture($
         
     }
   }
-}
\ No newline at end of file
+}
+
+if (module_exists('actions')) {
+
+  /**
+   * Implementation of hook_hook_info()
+   */
+  function privatemsg_hook_info() {
+    return array(
+      'privatemsg'  =>  array(
+        'privatemsg'  =>  array(
+          'privatemsg sent' => array(
+            'runs when' => t('After a Private Message was sent.'),
+          ),
+        ),
+      ),
+    );
+  }
+  
+  /**
+   * Implementation of hook_action_info()
+   */
+  function privatemsg_action_info() {
+    return array(
+      'privatemsg_send_notification_action'  => array(
+        'description'   =>  t('Send an e-mail to the recipient of the private message'),
+        'type'          =>  'privatemsg',
+        'configurable'  =>  TRUE,
+        'hooks'         =>  array(
+          'privatemsg'  =>  array('privatemsg sent'),
+        ),
+      ),
+    );
+  }
+  
+  /**
+   * privatemsg_send_notification - action configuration form
+   */
+  function privatemsg_send_notification_action_form($context) {
+    $form['subject'] = array(
+      '#type'           =>  'textfield',
+      '#title'          =>  t('E-mail subject'),
+      '#description'    =>  t('Enter here the e-mail subject'),
+      '#default_value'  =>  $context['subject'],
+    );
+    $form['body'] = array(
+      '#type'           =>  'textarea',
+      '#title'          =>  t('E-mail body'),
+      '#description'    =>  t('Enter here the body of the e-mail message that you want to sent as notification to the recipient\'s address. You may include the following variables: %site_name, %msg_id, %msg_link, %msg_time, %subject, %message, %sender_username, %sender_uid, %sender_mail, %recipient_username, %recipient_uid, %recipient_mail.'),
+      '#default_value'  =>  $context['body'],
+    );
+    
+    $result = db_query("SELECT category, name, title FROM {profile_fields}");
+    if (db_num_rows($result)) { 
+      $fields = array('-1' => '-select a profile field-');
+      while ($row = db_fetch_object($result)) {
+        $fields[$row->category][$row->name] = $row->title . ' (' . $row->name . ')';
+      }
+      $form['profile_check'] = array(
+        '#type'           =>  'select',
+        '#title'          =>  t('Profile field to disable notifications'),
+        '#options'        =>  $fields,
+        '#default_value'  =>  $context['profile_check'],
+        '#description'    =>  t('Choose a profile field from the recipient\'s profile that needs to be checked (or have a non-empty value) in order to disable e-mail notifications. For example a select here a checkbox that user can toggle in order to receive or not e-mail nnotifications.'),
+      );
+    }
+    return $form;
+  }
+
+  /**
+   * privatemsg_send_notification - action configuration form submit
+   */
+  function privatemsg_send_notification_action_submit($form_id, $form_values) {
+    return array(
+      'subject'       =>  $form_values['subject'],
+      'body'          =>  $form_values['body'],
+      'profile_check' =>  $form_values['profile_check'],
+    );
+  }
+  
+  /**
+   * privatemsg_send_notification - the action itself
+   */
+  function privatemsg_send_notification_action($message = NULL, $context) {
+    $privatemsg = $message;
+    switch ($context['hook']) {
+      case 'privatemsg':
+        $privatemsg = $context['message'];
+      break;
+    }
+    $sender = user_load(array('uid' => $privatemsg->author));
+    $recipient = user_load(array('uid' => $privatemsg->recipient));
+
+    if ($context['profile_check'] != '-1' && isset($context['profile_check'])) {
+      $field = $context['profile_check'];
+      if ($recipient->$field) {
+        return;
+      }
+    }
+
+    $vars = array(
+      '%site_name'          =>  variable_get('site_name', 'Drupal'),
+      '%msg_id'             =>  $privatemsg->id,
+      '%msg_link'           =>  url('privatemsg/view/' . $privatemsg->id, NULL, 'message-' . $privatemsg->id, TRUE),
+      '%msg_time'           =>  format_date($privatemsg->timestamp),
+      '%subject'            =>  $privatemsg->subject,
+      '%message'            =>  $privatemsg->message,
+      '%sender_username'    =>  $sender->name,
+      '%sender_uid'         =>  $sender->uid,
+      '%sender_mail'        =>  $sender->mail,
+      '%recipient_username' =>  $recipient->name,
+      '%recipient_uid'      =>  $recipient->uid,
+      '%recipient_mail'     =>  $recipient->mail,
+    );
+    $subject = strtr($context['subject'], $vars);
+    $subject = str_replace(array("\r", "\n"), '', $subject);
+    $message = strtr($context['body'], $vars);
+    $from = variable_get('site_mail', ini_get('sendmail_from'));
+  
+    if (drupal_mail('action_send_notification', $recipient->mail, $subject, $message, $from)) {
+      drupal_set_message('The user was notified by e-mail');
+      watchdog('action', t('Sent email to user %name', array('%name' => $recipient->name)));
+    }
+    else {
+      watchdog('error', t('Unable to send email to %name', array('%name' => $recipient->name)));
+    }
+  }
+}
+
+/**
+ * Implementation of hook_privatemsg()
+ */
+function privatemsg_privatemsg($op, $message = NULL) {
+  switch ($op) {
+    case 'privatemsg sent':
+      $aids = _actions_get_hook_aids('privatemsg', $op);
+      $context = array(
+        'hook'    =>  'privatemsg',
+        'op'      =>  $op,
+      );
+      foreach ($aids as $aid => $action_info) {
+        if ($action_info['type'] == 'privatemsg') {
+          $context['message'] = $message;
+        }
+        actions_do($aid, $message, $context);
+      }
+    break;
+  }
+}
