Private Message Function in while loop

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Post Reply
marc77
Registered User
Posts: 22
Joined: Sun Nov 02, 2008 12:02 pm

Private Message Function in while loop

Post by marc77 »

I have to send some Users a private Message. Therefore I make a select to the database:

Code: Select all

$sql = "SELECT * FROM `phpbb3_users` WHERE `user_email` LIKE '%[email protected]%'"; 

$result = $db->sql_query($sql);
The select has 17 results.

Then I use this function to send a private Message to each user_id

Code: Select all

include($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);

function sendphpbbpm($pmmessage,$mid,$pmsubject) {

global $config, $db, $user, $auth, $template, $phpbb_root_path, $phpEx;

$message = utf8_normalize_nfc($pmmessage);
$uid = $bitfield = $options = ''; // will be modified by 
generate_text_for_storage
$allow_bbcode = $allow_smilies = true;
$allow_urls = true;
generate_text_for_storage($message, $uid, $bitfield, $options, 
$allow_bbcode, $allow_urls, $allow_smilies);
$pm_data = array(
'from_user_id'      => '45148', //Community
'from_user_ip'      => '127.0.0.1',
'from_username'     => 'Community',
'enable_sig'        => false,
'enable_bbcode'     => true,
'enable_smilies'    => true,
'enable_urls'       => false,
'icon_id'           => 0,
'bbcode_bitfield'   => $bitfield,
'bbcode_uid'        => $uid,
'message'            => $message,
'address_list'        => array('u' => array($mid => 'to')),
 );

//Now We Have All Data Lets Send The PM!!
submit_pm('post', $pmsubject, $pm_data, false, false);
}
In the while loop I use the function call:

Code: Select all

while ($row = $db->sql_fetchrow($result))
{

$mid = $row['user_id'];
$pmsubject = 'Test Subject';
$pmmessage = 'Test';

sendphpbbpm($pmmessage,$mid,$pmsubject);

}

The problem is: Only the first user_id get a private message and no one else. Any Ideas?
Last edited by HiFiKabin on Mon Feb 05, 2018 11:01 am, edited 1 time in total.
Reason: Moved to Custom Coding
User avatar
Ger
Registered User
Posts: 2108
Joined: Wed Jan 02, 2008 7:35 pm
Location: 192.168.1.100
Contact:

Re: Private Message Function in while loop

Post by Ger »

What version of phpBB are you using? submit_pm() has only 4 parameters.
My extensions:
Simple CMS, Feed post bot, Avatar Resize, Modbreak, Magic OGP, Live topic update, Modern Quote, Quoted Where (GDPR) and Autoresponder.
Newest: FAQ manager for 3.2

Like my work? Buy me a coffee to keep it coming. :ugeek:

-Don't PM me for support-
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Private Message Function in while loop

Post by mrgoldy »

And why not use the 'address list'?

Code: Select all

$address_list = array();

while ($row = $db->sql_fetchrow($result))
{
	$address_list[$row['user_id']] = 'to';
}
And then in your $pm_data just use:

Code: Select all

$pm_data = array(
'address_list'	=> array('u' => $address_list),
);
Also, I would not use the * on users table, as you then also fetch passwords, etc. It's better to only fetch the fields you need, ie. user_id.
Moreover, Ger is right in saying that the submit_pm function only uses 4 arguments, includes/functions_privmsg.php:

Code: Select all

function submit_pm($mode, $subject, &$data_ary, $put_in_outbox = true)
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
Post Reply

Return to “phpBB Custom Coding”