MyBB Community Forums

Full Version: Using PHP to send a pm.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
The function is being called at the end of a script that's used to approve or reject a submission to the site. So for example, the accept button is clicked, the script updates the database, and then the sendPM function is called to inform the user who submitted the item in question that it has been accepted.
What about this, cahnge this code of yours:
    if($pmhandler->validate_pm())
    {
        $pmhandler->insert_pm();
        return true;
    }
    else
    {
        return false;
    }

For this:
	if(!$pmhandler->validate_pm())
	{
		$pmhandler->is_validated = true;
		$pmhandler->errors = array();
	}
	$pmhandler->insert_pm();
Thanks for the suggestion but you seem to just be overriding the validation check and forcing it to send a PM but that's not the problem. validate_pm() is returning true and insert_pm() is returning messagesent => 1 so everything thinks it's succeeding. The only part that is broken is that the PM is never being received.

Additionally, dumping the $pm variable from the function returns the following, which appears to be populated correctly:


Array
(
    [subject] => Sheet Approved!
    [message] => Congratulations! Your sheet was approved!
    [icon] => -1
    [toid] => 16368
    [fromid] => 9578
    [do] => 
    [pmid] => 
    [options] => Array
        (
            [signature] => 0
            [disablesmilies] => 0
            [savecopy] => 0
            [readreceipt] => 0
        )
)
We're still really stuck on this one - the rest of the approval system has been completed, except the PM sending... Sorry to keep bumping this up!
Hey, just checking in once more to see if anyone has any more ideas here. I'm not one to usually bump threads but it's no longer on the first page and we're still pretty stuck so any help would be appreciated!
I believe the toid has to be an array.
Take a look how Pirata's VIP Membership plugin sends PM, although its for 1.4x, the PM code should work fine. You can find it on mods site. I'm not sure whether I could paste the PM code from it here or not due to distribution issues, so I ask you to check it personally via mods site.
labrocca and crazy4cs, you have both been integral in finally solving this problem! I didn't think it made sense that toid would be required to be an array but when I checked the PM code from the VIP Membership plugin, that's exactly how it was set up. I tried it and it worked! Thank you both, as well as everyone else who helped us!

For anyone who happens to stumble upon this thread and is curious or would like a working function pre-written, here's what I used (with a slight modification to the include path to make it more generic):

  function sendPM($subject, $message, $to, $from)
  {
      global $mybb
      require_once(MYBB_ROOT . "inc/datahandlers/pm.php");
      $pmhandler = new PMDataHandler();
      $pmhandler->admin_override = true;

      $pm = array(
          "subject" => $subject,
          "message" => $message,
          "icon" => "-1",
          "toid" => array(),
          "fromid" => $from,
          "do" => "",
          "pmid" => ""
      );
      $pm["options"] = array(
          "signature" => "0",
          "disablesmilies" => "0",
          "savecopy" => "0",
          "readreceipt" => "0"
      );
      
      if(!is_array($to))
      {
          array_push($pm["toid"], $to);
      }
      else
      {
          foreach($to as $uid)
          {
              array_push($pm["toid"], $uid);
          }
      }

      $pmhandler->set_data($pm);

      if($pmhandler->validate_pm())
      {
          $pmhandler->insert_pm();
          return true;
      }
      else
      {
          return false;
      }
  }
Is there any way to modify this script to send the PM to an entire group?
You will need to get the users (UIDs) from that group (GIDs).
Pages: 1 2 3