MyBB Community Forums

Full Version: Send PM when needed
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have created my own staff application page, created with PHP. What I want, is when a user is accepted, I want the system to automatically send the user a private message, telling them they have been accepted.

I know it probably has something to do with the PM data handler, but I'm not sure how I would go by creating the script to send it.

Thanks
The pmonreg_run() function in the PM on Registration plugin should explain it, post back if you need any of it explained in more detail.
Ok, so this part, correct??

function pmonreg_run()
{
	global $mybb, $user_info, $cache, $user, $lang;

	if($mybb->settings['pmonreg_switch'] != 0)
	{
		require_once MYBB_ROOT."inc/datahandlers/pm.php";
	
		$pmhandler = new PMDataHandler();
		
		$recipients_to = array($user_info['uid']);
		$recipients_bcc = array();
		
		// Handle CC list
		$cc_list = explode(',', $mybb->settings['pmonreg_cc_uids']);
		foreach($cc_list as $cc_uid)
		{
			if(intval($cc_uid))
			{
				$recipients_to[] = intval($cc_uid);
			}
		}
		
		// Handle BCC List
		$bcc_list = explode(',', $mybb->settings['pmonreg_bcc_uids']);
		foreach($bcc_list as $bcc_uid)
		{
			if(intval($bcc_uid))
			{
				$recipients_bcc[] = intval($bcc_uid);
			}
		}
		
		// Handle subject/message from language pack
		$subject = $mybb->settings['pmonreg_subject'];
		if(isset($lang->pmonreg_subject))
		{
			$subject = $lang->pmonreg_subject;
		}
		
		$message = $mybb->settings['pmonreg_message'];
		if(isset($lang->pmonreg_message))
		{
			$message = $lang->pmonreg_message;
		}
		

		$pm = array(
			"subject" => str_replace('{username}', $user['username'], $subject),
			"message" => str_replace('{username}', $user['username'], $message),
			"icon" => -1,
			"fromid" => intval($mybb->settings['pmonreg_uid']),
			"toid" => $recipients_to,
			"bccid" => $recipients_bcc,
			"do" => '',
			"pmid" => ''
		);
	
		$pm['options'] = array(
			"signature" => $mybb->settings['pmonreg_showsignature'],
			"disablesmilies" => $mybb->settings['pmonreg_disablesmilies'],
			"savecopy" => $mybb->settings['pmonreg_savecopy'],
			"readreceipt" => $mybb->settings['pmonreg_receipt']
		);
		$pm['saveasdraft'] = 0;
		$pmhandler->admin_override = 1;
		$pmhandler->set_data($pm);
		if($pmhandler->validate_pm())
		{
			$pmhandler->insert_pm();
		}
		else
		{
			pmonreg_handle_error(&$pmhandler);
		}
	}
}

Then I can just edit the array, to my needs?

$pm = array(
			"subject" => str_replace('{username}', $user['username'], $subject),
			"message" => str_replace('{username}', $user['username'], $message),
			"icon" => -1,
			"fromid" => intval($mybb->settings['pmonreg_uid']),
			"toid" => $recipients_to,
			"bccid" => $recipients_bcc,
			"do" => '',
			"pmid" => ''
		);

If so, I can edit the 'fromid' to my bots uid? I can also do the same for the message part? Just do "message" => 'message here'

Thanks
Yep that should be pretty much it but you will probably need to include global.php as well.
Ok, I currently have this, and it's not functioning...

if(isset($_POST['reject']))
{
	$uid = mysql_real_escape_string($_POST['uid']);
	
	$query = mysql_query("UPDATE `mybb_users` SET app_status='5' WHERE uid='$uid'");
	$next = mysql_query("DELETE FROM `mybb_apps_pending` WHERE uid='$uid'");
	$last = mysql_query("DELETE FROM `mybb_apps_clarify` WHERE uid='$uid'");
	
	$reject_subject = 'Staff Application Rejected';
	$reject_message = 'Sorory';
	
	send_pm();
	
	header('Location: /volunteer');
}

That's the script that needs to run, and here's the pm data handler...

<?php
define('IN_MYBB', 1); 
require "../global.php";

function send_pm()
{
    global $mybb, $user_info, $cache, $user, $lang;

    if($mybb->settings['pmonreg_switch'] != 0)
    {
        require_once MYBB_ROOT."../inc/datahandlers/pm.php";
    
        $pmhandler = new PMDataHandler();
        
        $recipients_to = array($user_info['uid']);
        $recipients_bcc = array();
        
        // Handle CC list
        $cc_list = explode(',', $mybb->settings['pmonreg_cc_uids']);
        foreach($cc_list as $cc_uid)
        {
            if(intval($cc_uid))
            {
                $recipients_to[] = intval($cc_uid);
            }
        }
        
        // Handle BCC List
        $bcc_list = explode(',', $mybb->settings['pmonreg_bcc_uids']);
        foreach($bcc_list as $bcc_uid)
        {
            if(intval($bcc_uid))
            {
                $recipients_bcc[] = intval($bcc_uid);
            }
        }
        
        // Handle subject/message from language pack
        $subject = $mybb->settings['pmonreg_subject'];
        if(isset($lang->pmonreg_subject))
        {
            $subject = $reject_subject;
        }
        
        $message = $mybb->settings['pmonreg_message'];
        if(isset($lang->pmonreg_message))
        {
            $message = $reject_message;
        }
        

        $pm = array(
            "subject" => str_replace('{username}', $user['username'], $subject),
            "message" => str_replace('{username}', $user['username'], $message),
            "icon" => -1,
            "fromid" => '2',
            "toid" => $recipients_to,
            "bccid" => $recipients_bcc,
            "do" => '',
            "pmid" => ''
        );
    
        $pm['options'] = array(
            "signature" => $mybb->settings['pmonreg_showsignature'],
            "disablesmilies" => $mybb->settings['pmonreg_disablesmilies'],
            "savecopy" => $mybb->settings['pmonreg_savecopy'],
            "readreceipt" => $mybb->settings['pmonreg_receipt']
        );
        $pm['saveasdraft'] = 0;
        $pmhandler->admin_override = 1;
        $pmhandler->set_data($pm);
        if($pmhandler->validate_pm())
        {
            $pmhandler->insert_pm();
        }
        else
        {
            pmonreg_handle_error(&$pmhandler);
        }
    }
} 
?>
Can anyone help me on this??
Does it return any errors?
No, no errors - It just doesn't send it.
Instead of pmonreg_handle_error, do something like die("invalid pm"). If you see the die, then you need to re-look at what you've put into the PM.
Instead of pmonreg_handle_error, do something like die("invalid pm"). If you see the die, then you need to re-look at what you've put into the PM.