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
We're programming a site that will use MyBB to handle user accounts.

I'll try to make it more "generally understood" by relating it to a photo gallery. People upload things to our database, and we accept or deny the image being displayed on the site.

We're trying to set up the site to send out a PM when something is accepted or denied, instead of sending an email. This makes the site feel more complete, in my opinion.

Is there an easy way of sending a pm through php, or will it be quite a task? Has anyone done it before with success?

Any help would be greatly appreciated.
sending automatic PMs is possible in MyBB ; are you using a gallery system available for MyBB (i.e. like a plugin) OR is it not related to MyBB ..
It isn't a plugin. It's a separate site being written to tie into MyBB's database (i.e. making use of the $mybb object for user information and privileges on the main, non-forum site).
The following should suffice. Edit to your needs.

		require_once MYBB_ROOT."inc/datahandlers/pm.php";
		$pmhandler = new PMDataHandler();
		$pmhandler->admin_override = true;

		$pm = array(
			"subject" => 'Subject',
			"message" => 'Message',
			"icon" => "-1",
			"toid" => 1,
			"fromid" => 0,
			"do" => '',
			"pmid" => ''
		);
		$pm['options'] = array(
			"signature" => "0",
			"disablesmilies" => "0",
			"savecopy" => "0",
			"readreceipt" => "0"
		);
	
		$pmhandler->set_data($pm);
				
		if($pmhandler->validate_pm())
		{
			$pmhandler->insert_pm();
		}

toid will be the uid of the user you're sending the message to, and fromid is the uid of the user who's sending the message respectively. If you leave it as 0 then it will be sent from "MyBB Engine", a non existent system account.
That's perfect. Thank you very much, Nathan!
Edit : not seen Malcolm's response ..

I can only suggest to trace how automatic PMs are sent on a specific event by using a couple of plugins ...
you can see A2 Detector , Automatic PM on Move/Rename/Delete Thread ...
We appreciate your help all the same, ranjani! The plugins you listed may still prove useful later anyway so the information is welcome regardless.
Hey again! So I just got around to testing this and it doesn't seem to be working. I used the code provided in a custom function as follows:

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

    $pm = array(
        "subject" => $subject,
        "message" => $message,
        "icon" => "-1",
        "toid" => $to,
        "fromid" => $from,
        "do" => "",
        "pmid" => ""
    );
    $pm["options"] = array(
        "signature" => "0",
        "disablesmilies" => "0",
        "savecopy" => "0",
        "readreceipt" => "0"
    );

    $pmhandler->set_data($pm);

    if($pmhandler->validate_pm())
    {
        $pmhandler->insert_pm();
        return true;
    }
    else
    {
        return false;
    }
}

$path is being calculated further up in the same file and it's loading the file from MyBB correctly. This function is later being called from another file as follows:

sendPM("Sheet Approved!", "Congratulations! Your sheet was approved!", $pm["uid"], $mybb->user["uid"]);

$pm["uid"] is a result of a query used to get the user ID of the person who submitted the sheet and testing confirmed that it's getting that value correctly.

Trying the function in an if resulted in it returning true so it thinks it's succeeding but I don't see any new PMs. Any ideas? Thanks in advance!
Hey, we're still looking for help on this if anyone is available to help out? We're still unsure...
What are you triggering the function with? I mean, when does it send the pm, or supposed to send the pm?
Pages: 1 2 3