When using privatemsg_new_thread what works for $recipients? I have the message field. The author as null defaults to the current user which is fine. Subject is easy to change.. The message shows up in the sent box for the current user but the recipient is "Anonymous"...

i.e. if programmer is the current user:
Participants: Anonymous and programmer

I have tried everything that I can think of.. User uid, name, user_load($node->uid)

Comments

berdir’s picture

Status: Active » Postponed (maintainer needs more info)

http://blog.worldempire.ch/api/function/privatemsg_new_thread/1

$recipients is an array of user objects, for example:

$recipients = array(user_load(1));

Can you please paste the code that sent a message to anonymous? That should not be possible, the validate function should check if all recipients are valid user objects.

berdir’s picture

Updated the above link with a short example.

Also, make sure to have a look at #375999: #288183 followup: Developer friendlify privatemsg_new_thread API. because that patch will change the order of the parameters.

nathaniel’s picture

Excellent that works perfectly! Thank you for the quick response and explanation...

What I am trying to do is integrate Privatemsg so that when a user requests to join a group (og module) the group owner will receive a private message alerting them of the request.

I am using the latest dev release for Privatemsg (2009-Mar-06). At first I just dropped $admins in for the recipients and it was sending messages to "Anonymous".

$recipients = array(user_load($node->uid)); works

$admins sends to "Anonymous"

Here is the code from the og module beginning around line 980:

<?php
function og_subscribe_user($gid, $account, $request = NULL) {
  // moderated groups must approve all members (selective=1)
  $node = node_load($gid);
  switch ($node->og_selective) {
    case OG_MODERATED:
      $admins = array();
      og_save_subscription($gid, $account->uid, array('is_active' => 0));
      $sql = og_list_users_sql(1, 1, NULL);
      $res = db_query($sql, $node->nid);
      $admins = array();
      while ($row = db_fetch_object($res)) {
        $admins[] = $row->uid;
      }
      if (!empty($admins)) {
        // Prepend user's request text with standard cruft. Should be a
        // variable but that's a bit annoying.
        if ($request) {
          $request = t("\n\nPersonal message from @name:\n------------------\n\n@request", array('@name' => $account->name, '@request' => $request));
        }
		
        $variables = array(
          '@group' => $node->title,
          '@username' => $account->name,
          '!approve_url' => url("og/approve/$node->nid/$account->uid", array('absolute' => TRUE)),
          '!group_url' => url("og/users/$node->nid", array('absolute' => TRUE)),
          '@request' => $request,
        );
        
        $message = array(
          'subject' => _og_mail_text('og_request_user_subject', $variables),
          'body' => _og_mail_text('og_request_user_body', $variables) . $request
        );
		
		// integrate privatemsg
		privatemsg_new_thread('Group Request', $request, $admins, $author = NULL);

        // Send notifications to each admin; Sending an array of recipients
        // implies that this is a bulk message.
        module_invoke_all('og', 'user request', $gid, $admins, $message);
      }
      $return_value = array('type' => 'approval',
                            'message' => t('Membership request to the %group group awaits approval by an administrator.', array('%group' => $node->title)));
?>
berdir’s picture

Title: For privatemsg_new_thread what is $recipients? » Validate $recipients (only user objects are allowed, not uid's)
Category: support » bug
Status: Postponed (maintainer needs more info) » Active

1) $admins is an array of uids, you can use the user objects with the following code:

$admin_recipients = array_map('user_load', $admins);

2) It seems that og is executing a hook, you could use that to send messages instead of changing the code of og. Untested example code:

function mymodule_og($op, $gid, $admins, $message) {
  if ($op = 'user request') {
    privatemsg_new_thread($message['subject'], $message['body'], array_map('user_load', $admins));
  }
}

I am changing the thread to a bug report, it seems that the validate function does not correctly validate the recipients array.

berdir’s picture

StatusFileSize
new1.04 KB

Attached is a patch which should detect if you pass anything else than a user object with an uid property.

Edit: This needs to be ro-rolled after #375999: #288183 followup: Developer friendlify privatemsg_new_thread API. (or the other way round), because the patch uses the old error format.

berdir’s picture

Status: Active » Needs review
litwol’s picture

Status: Needs review » Needs work
berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new1.09 KB

Done.

berdir’s picture

Status: Needs review » Closed (won't fix)

Drupal core does not test/validate proper params so probably we shouldn't either. Setting this to won't fix.