How to send a notification email when a PM is sent.

Note that this functionality is now built in to the module (as of Jan. 26, 2007). Users can now go in their preferences and choose whether to receive no email, a daily email or an email for every privatemsg.

For this I had to edit the PM module file. I did not like the way that it sent messages via cron only once a day.

I added the following to the top of the module file.

function sendnotify($recipient) {

$from = variable_get('site_mail', ini_get('sendmail_from'));
$subject = t('New private messages at %site.', array('%site' => variable_get('site_name', 'drupal')));
$message = t('Hi %name,
This is an automatic reminder from the site %site. You have %new unread private messages.

To read your messages, follow this link:
%link1

', array('%name' => $user->name, '%site' => variable_get('site_name', 'drupal'), '%new' => $alert->c, '%link1' => url('user/login', 'destination=privatemsg', NULL, 1), '%link2' => url('user/'. $user->uid .'/edit', NULL, NULL, 1)));

user_mail($recipient->mail, $subject, $message, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
}

and then I added the function call sendnotify($recipient) in the function called function _privatemsg_edit($edit) I added this in the else block of the privatemsg_edit function the code for the entire function is below.

function _privatemsg_edit($edit) {
  global $user;

  if ($edit['recipient'] == '') {
    form_set_error('recipient', t('The <em>Recipient</em> field is required.'));
    return _privatemsg_form((object)$edit);
  }
  else {
    $recipient = user_load(array('name' => $edit['recipient']));
  }
 
  if (!$recipient->uid) {
    form_set_error('recipient', t('The <em>Recipient</em> does not exist.'));
    return _privatemsg_form((object)$edit);
  }
  else if (!(isset($recipient->privatemsg_allow) ? $recipient->privatemsg_allow : 1)) {
    form_set_error('recipient', t('%name does not accept private messages.', array('%name' => $recipient->name)));
    return _privatemsg_form((object)$edit);
  }
  else if (trim($edit['subject']) == '') {
    form_set_error('subject', t('The <em>Subject</em> field is required.'));
    return _privatemsg_form((object)$edit);
  }
  if (trim($edit['privatemsgbody']) == '') {
    form_set_error('privatemsgbody', t('The <em>Message</em> field is required.'));
    return _privatemsg_form((object)$edit);
  }
  else if (array_key_exists('format', $edit) && !filter_access($edit['format'])) {
    form_set_error('format', t('The supplied input format is invalid.'));
    return _privatemsg_form((object)$edit);
  }
  else {
    $result = db_query("INSERT INTO {privatemsg} (author, recipient, subject, message, timestamp, newmsg, hostname, format) VALUES ('%d', '%d', '%s', '%s', '%d', '%d', '%s', '%d')", $user->uid, $recipient->uid, $edit['subject'], $edit['privatemsgbody'], time(), 1, getenv("REMOTE_ADDR"), $edit['format']);
    drupal_set_message(t('Message sent.'));
sendnotify($recipient);
    drupal_goto($user->uid ? 'privatemsg' : '');
  }
}

This code has not been thoroughly tested I have tested it a couple times and it works and really is kind of simple to the point where I dont see why it wouldnt work or that it would break. You should also get rid of the email notification in the cron function since it would no longer be needed. Let me know how this works, this is my first module hack.

-Kyle

 
 

Drupal is a registered trademark of Dries Buytaert.