MyBB Community Forums

Full Version: Send PM function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I made this function as some of my plugins use the private message system.
I decided to share it others as looks like everyone is sharing their functions too.
More functions will probably help other developers.

So here it is:
/**
 * Sends a PM to a user
 * 
 * @param array: The PM to be sent; should have 'subject', 'message', 'touid' and 'receivepms'
 * (receivepms is for admin override in case the user has disabled pm's)
 * @param int: from user id (0 if you want to use the uid of the person that sends it. -1 to use MyBB Engine
 * @return bool: true if PM sent
 */
function mypluginname_send_pm($pm, $fromid = 0)
{
	global $lang, $mybb, $db;
	if($mybb->settings['enablepms'] == 0) return false;
	if (!is_array($pm))	return false;
	if (!$pm['subject'] ||!$pm['message'] || !$pm['touid'] || !$pm['receivepms']) return false;
	
	$lang->load('messages');
	
	require_once MYBB_ROOT."inc/datahandlers/pm.php";
	
	$pmhandler = new PMDataHandler();
	
	$subject = $pm['subject'];
	$message = $pm['message'];
	$toid = $pm['touid'];
	
	require_once MYBB_ROOT."inc/datahandlers/pm.php";

	$pmhandler = new PMDataHandler();
	
	if (is_array($toid))
		$recipients_to = $toid;
	else
		$recipients_to = array($toid);
	$recipients_bcc = array();
	
	if (intval($fromid) == 0)
		$fromid = intval($mybb->user['uid']);
	elseif (intval($fromid) < 0)
		$fromid = 0;

	$pm = array(
		"subject" => $subject,
		"message" => $message,
		"icon" => -1,
		"fromid" => $fromid,
		"toid" => $recipients_to,
		"bccid" => $recipients_bcc,
		"do" => '',
		"pmid" => ''
	);

	$pm['options'] = array(
		"signature" => 0,
		"disablesmilies" => 0,
		"savecopy" => 0,
		"readreceipt" => 0
	);
	$pm['saveasdraft'] = 0;
	$pmhandler->admin_override = 1;
	$pmhandler->set_data($pm);
	if($pmhandler->validate_pm())
	{
		$pmhandler->insert_pm();
	}
	else
	{
		return false;
	}
	
	return true;
}
That from the buddy system?
Thanks for sharing the code;

I have one request, can you also please post a function of Auto Post in a specific forum ?
(2009-09-04, 01:54 AM)ghazal Wrote: [ -> ]I have one request, can you also please post a function of Auto Post in a specific forum ?

Uhm, use the post datahandler and set the FID as you need to? I'm sure you're more than able to give that ago, Ghazal. Why not have a go at it, and let us know how you get on?

Shy
(2009-09-03, 11:32 PM)T0m Wrote: [ -> ]That from the buddy system?

the buddy system one is a bit different because it has an alternative PM system so depending on the settings, it sends a regular PM or an "alternative" one.


(2009-09-04, 01:54 AM)ghazal Wrote: [ -> ]Thanks for sharing the code;

I have one request, can you also please post a function of Auto Post in a specific forum ?

Just like tomm said, use the post handler to do it. Just take a look at the post handler file:
inc/datahandlers/post.php
(by the way Tomm, shouldn't the email datahandler be here(in the datahandlers folder) too? it's called inc/class_mailhandler.php
As for "auto" post I think you mean auto post something each X minutes or something.
I already told you how to do it via PM when you asked me.

Just create a table where you keep logging the lasttime it auto posted and then compare it like 'last time posted' + 'number of seconds to wait' < TIME_NOW (TIME_NOW is defined in global.php = time() ) and auto post another one
(2009-09-04, 10:29 AM)Pirata Nervo Wrote: [ -> ](by the way Tomm, shouldn't the email datahandler be here(in the datahandlers folder) too? it's called inc/class_mailhandler.php

Uhm, no lol.

The datahandlers are extended classes of ./inc/datahandler.php. The mailhandler is a class of it's own because it's simply that good.

Shy
Oh okay Toungue
I can't get it to work. Is it compatible with 1.6 ? Can someone post an example?
(2010-09-04, 07:01 PM)pleonidas Wrote: [ -> ]I can't get it to work. Is it compatible with 1.6 ? Can someone post an example?

After a few hours... Smile
For anyone having problems (no error message, pm not sent)
check the validation functions of the pmhandler
(eg. subject should not be over 85chars etc....)