MyBB Community Forums
Using PHP to send a pm. - Printable Version

+- MyBB Community Forums (https://community.mybb.com)
+-- Forum: Extensions (https://community.mybb.com/forum-201.html)
+--- Forum: Plugins (https://community.mybb.com/forum-73.html)
+---- Forum: Plugin Development (https://community.mybb.com/forum-68.html)
+---- Thread: Using PHP to send a pm. (/thread-114471.html)

Pages: 1 2 3


Using PHP to send a pm. - tsrdazz - 2012-03-04

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.


RE: Using PHP to send a pm. - ranjani - 2012-03-04

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 ..


RE: Using PHP to send a pm. - Petie - 2012-03-04

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).


RE: Using PHP to send a pm. - Nathan Malcolm - 2012-03-04

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.


RE: Using PHP to send a pm. - Petie - 2012-03-04

That's perfect. Thank you very much, Nathan!


RE: Using PHP to send a pm. - ranjani - 2012-03-04

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 ...



RE: Using PHP to send a pm. - Petie - 2012-03-04

We appreciate your help all the same, ranjani! The plugins you listed may still prove useful later anyway so the information is welcome regardless.


RE: Using PHP to send a pm. - Petie - 2012-04-15

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!


RE: Using PHP to send a pm. - tsrdazz - 2012-04-21

Hey, we're still looking for help on this if anyone is available to help out? We're still unsure...


RE: Using PHP to send a pm. - Frank.Barry - 2012-04-21

What are you triggering the function with? I mean, when does it send the pm, or supposed to send the pm?