4

I also opened a thread on this on Drupal.org but since it's been awhile I thought I'd try asking here.

Use Case: In general, I want users to receive notifications for new private messages. I can do this using the included submodule for e-mailing private messages.

So that the private message inbox has a message when users log in for the first time, I used the Rules module to automatically send a "welcome" private message upon registration on behalf of an administrator account. However, this triggers the "send e-mail on private message" behavior. Since users already are receiving a "please validate your e-mail address" e-mail, I don't want them to receive a second e-mail notifying them that they have received the "Welcome" private message.

Is there a way to prevent messages from certain users (or, alternately, certain messages) from triggering the e-mail notification?

I also looked into implementing this using rules instead of the included submodule but this would mean I couldn't use the functionality for reducing the number of notifications (currently, I have it set to not send additional e-mails if the user has yet to log in and read the first e-mail in a thread from a given user).

  • “I have an administrator account that automatically sends a "Welcome" private message when users join the site” — in which way it has done, Rules or some module? – kalabro May 20 '12 at 14:57
  • Using Rules. I have updated the question. – Patrick Kenny May 20 '12 at 15:06
  • I added an answer with example for Rules. – kalabro May 21 '12 at 15:33
4
+150

Note: this is answer for Drupal 7.

As Letharion said, you can use hook_mail_alter() but you should detect somehow is this a welcome message from admin or not.
I suggest Context module for this task, but you really can just compare a subject of message in this hook.

Update

First I suggested to use Context module, but it is no so simple to set context in Rules. If you are interested in that way, you can see history of an answer.
Current code is using information about Author id and subject of message:

/**
 * Implements hook_mail_alter().
 */
function YOURMODULE_mail_alter(&$message) {
  $WELCOME_USER_ID = 1; // PM author from your rule
  $WELCOME_SUBJECT = 'Welcome to the site'; // PM subject from your rule
  // 'pm_email_notify_notice' is an ID for message notifications
  // from Privatemsg Email Notification module
  if($message['id'] == 'pm_email_notify_notice') {
    $privatemsg = $message['params']['message'];
    if ($privatemsg->author->uid == $WELCOME_USER_ID && $privatemsg->subject == $WELCOME_SUBJECT) {
    // If special context is set it means that we don't want to send an email
    //if(context_isset('context', 'my_privatemsg_stop_email')) {
      // Stop email
      $message['send'] = FALSE;
    }
  }
}
  • Very cool solution! Is it necessary though to even mention the parts requiring eval()? The only ones who end up using eval are those that don't know why they shouldn't. I so wish we didn't consider eval the answer to things that have a proper solution. – Letharion May 20 '12 at 17:25
  • @Letharion, I just tried to show that the code to set a context is very simple (just one line). Here Rules are not so simple that's why the answer is large. – kalabro May 20 '12 at 17:40
  • 2
    Yes, I realize that. :) It's just that wrong people will end up actually using the code in the wrong way, and down the road be sad for it. Obligatory link to question about why it's bad to use eval(). – Letharion May 20 '12 at 19:18
  • Thank you, but is there a way to do this without Context? The Context Rules module hasn't had any updates in over a year and is clearly marked "not ready for production" (from that module's page). My site is about to go live so I need a more stable solution. – Patrick Kenny May 22 '12 at 11:32
  • I updated my answer with just comparing author and subject of Welcome PM. – kalabro May 22 '12 at 15:43
3

Using hook_mail_alter you can stop the sending from happening with $message['send'] = FALSE.

Unfortunately this gives no direct way of knowing that the mail is "the first mail", or "from uid 1". I believe that would need to be a match directly against the body text. The calling $module is however available, so you can limit the manipulation that way.

  • 1
    Both the message and the recipient should be available in the message context, see api.worldempire.ch/api/privatemsg/…, which should allow you to compare the author and recipient uid. – Berdir May 21 '12 at 16:57
  • Instead of the body text, is it possible to match against the subject? I tried calling the hook and using if ($message['subject'] == "New Message from help"), but I was unable to get it to work. (I am certain this will always be the subject for all cases in which I don't want to show the message.) – Patrick Kenny May 22 '12 at 11:30
  • Actually @Berdir's first comment seem even better, but it certainly should work either way. I can't help you debug "unable to get it to work" though, it's to vague. – Letharion May 22 '12 at 11:35
0

It could take some reengineering, but I would write a custom module that adds a row to the PM table (and any other linked tables, I don't have the PM database schema in front of me).

You could use rules to trigger the module after the "account has been created" event.

That might be more hand coding than you're wanting though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.