Hi,

I'm writing an action for a module, and using the privatemsg api. I'm experiencing two problems.

1: Cannot set author
The following code works, and sends a message from "Guest":

  privatemsg_new_thread(user_load($uid), $subject, $body);

However, the following code doesn't work:

  privatemsg_new_thread(user_load($uid), $subject, $body, array('author'=>user_load(1)));

This doesn't send a message at all.

Alternatively,

  privatemsg_new_thread(user_load($uid), $subject, $body, array('author'=>$object));

does send a message, but still from "Guest," not the user identified in the array.

2: no e-mail notification
Even when I don't set the author and the message gets through, e-mail notification is not triggered. Is there a separate function for this, or is this a bug?

Any pointers in the right direction would be greatly appreciated!

cheers

Comments

Berdir’s picture

Strange.

1. I'm pretty sure it should work like you're doing it. See our API tests: http://drupalcode.org/viewvc/drupal/contributions/modules/privatemsg/tes.... Are you getting any errors?

2. No tests to verify this, unfortunately, but it should work just fine. Try adding debug code in pm_email_notify.module in the _privatemsg_message_insert() hook to figure out why this doesn't work.

Berdir’s picture

Status: Active » Postponed (maintainer needs more info)
haikubear’s picture

Status: Postponed (maintainer needs more info) » Active

Hi Berdir,

Thanks for the quick response! I've been mucking around a bit and here's what I'm uncovering:

My code was actually sending the message to the currently logged in user, and from 'Guest' regardless of arguments I gave to privatemsg_new_thread. Here's some more of that code for context:

function mentions_notify_user_action($recipient, $context = array()) {
  // Set generic title and body of message
  $subject = t('Your input is requested!');
  $body    = t("Hi $recipient->name! You've been  mentioned in a post.");

  // Notify user using privatemsg API
  privatemsg_new_thread($recipient, $subject, $body);
}

Since it seemed like the function was using the global $user object as recipient instead of the object I passed it, I decided to set $user as follows:

  global $user;
  $olduser = $user;
  $user  = $recipient;
  // Notify user using privatemsg API
  privatemsg_new_thread($recipient, $subject, $body);
  $user = $olduser;

This helped to send the message to the correct user (i.e. the content of the $recipient object) - but the e-mail notifications still don't get triggered.

It's not logging any errors anywhere that I can find them. And I'm not sure how to add debug code... Thanks for all your help in wading through this!!

the code at the moment looks like this, just to have it all in one place:

function mentions_notify_user_action($recipient, $context = array()) {
  // Set generic title and body of message
  $subject = t('Your input is requested!');
  $body    = t("Hi $recipient->name! You've been  mentioned in a post.");
  global $user;
  $olduser = $user;
  $user  = $recipient;
  // Notify user using privatemsg API
  privatemsg_new_thread($recipient, $subject, $body);
  $user = $olduser;
}
Berdir’s picture

Status: Active » Fixed

Ah now I see it.

The first argument is an array of recipients, not a single recipient. Again, see the examples in the link I've provided and/or here: http://api.worldempire.ch/api/privatemsg/privatemsg.module/function/priv...

No idea why you end up with that strange result, but that should fix it.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Jarode’s picture

Status: Active » Closed (fixed)

Having exactly the same issue.

Works perfectly:

privatemsg_new_thread(array($user_object), $subject, $body);

Doesn't work:

privatemsg_new_thread(array($user_object), $subject, $body, array('author'=>$user_object));

Why ? I don't know. Very strange....

Jarode’s picture

Version: 6.x-1.3 » 7.x-1.3
Status: Closed (fixed) » Active

Status: Closed (fixed) » Active
ivnish’s picture

Issue summary: View changes
Status: Active » Closed (outdated)