Back to vBulletin 4 Articles

[How to] Send Auto PM in vb4.0
by bpr 03 May 2010

In this article I am going to explain you how to send a PM with the Vbulletin 4.0.X

it is based on this article
Send PMs (automatically)
written by Andreas

PHP Code:
            global $vbulletin;
            
            require_once(
DIR "/includes/class_dm.php");
            require_once(
DIR "/includes/class_dm_pm.php");
            
//pm system
            
$pmSystem   =   new vB_DataManager_PM$vbulletin );
            
            
//pm Titel / Text
            
$pnTitel    =   "Your Title for the PM";
            
$pnText     =   "This is pm which was created automatically. Please do not reply to this pm.";
            
            
$pmSystem->verify_message$pnText );
            
$pmSystem->verify_title$pnTitel );
            
            
//Set the fields
            
$pmSystem->set('fromuserid'1);
            
$pmSystem->set('fromusername''Meeting Bot');
            
$pmSystem->set('title'$pnTitel);
            
$pmSystem->set('message'$pnText);
            
$pmSystem->set('dateline'TIMENOW);
            
$pmSystem->set('iconid'4);
            
            
$pmSystem->set_recipients("bpr");

//"send" pn
            
if ( $pmSystem->pre_save() === false )
            {
                 if (
$pmSystem->errors) {
                    return 
$pmSystem->errors;
                } 
            }
            else
            {
                 
$pmSystem->save();               
            } 
That's it basically. Lets go through those lines:

Firstly it is to say, that you have to use the following three 3 lines if you are excluding that code in a class or function in a folder like /includes.
PHP Code:
            global $vbulletin;            
            require_once(
DIR "/includes/class_dm.php");
            require_once(
DIR "/includes/class_dm_pm.php"); 
Those will provide you the usage of the whole system. I assume, that you dont need those lines if you already included the global.php! - But I am not that shure maybe somebody could confirm that.

Anyway lets continue:
Before we can start setting anykind of information, we need a class with which we can work, this class is called "vB_DataManager_PM" it extends "vB_DataManager". (that is the reason why we had to require 2 classes from the /includes directory).

"vB_DataManager_PM" works with the table: "pmtext" of your database.

However we have to give her the registry, which is $vbulletin.
So it should like this:

PHP Code:
 $pmSystem   =   new vB_DataManager_PM$vbulletin ); 

from now on "$pmSystem" represents our class and we are finally able to set things up for sending a pm - so lets do it:

Before I actually set the message and the title to the PM I want to make shure, that it is ok and that there are no unallowed things inside.

PHP Code:
$pmSystem->verify_message$pnText );
            
$pmSystem->verify_title$pnTitel ); 
(Just to let you know if you did not recognize it, I was creating two variables for the text and the title earlier in the script I had shown above)

After that we do the actual PM set up:

PHP Code:
$pmSystem->set('fromuserid'1);
            
$pmSystem->set('fromusername''PM Bot');
            
$pmSystem->set('title'$pnTitel);
            
$pmSystem->set('message'$pnText);
            
$pmSystem->set('dateline'TIMENOW);
            
$pmSystem->set('iconid'4); 
$pmSystem->set_recipients("bpr");[/PHP]

We say:
  1. The PM is from the User with the ID 1
  2. The name of the sender is "PM Bot" - it doesn't have to be the name of the user with the id
  3. We set up the title!
  4. After that the message
  5. Now we need a date time when it was sent
  6. The iconid is not required, in this case it represents the red one with the ! symbol
  7. Who will get the message, you can provide more recipients by doing it as followed "user1;user2;user3;user4;..." Important is just the ; which identifies the list and another user


So lets send our PM or not?

Before we actually send we can check if it successes by:
PHP Code:
$pmSystem->pre_save() 
This will return "true" or "false" - obviously true represents that it will be successfull and false it wont!

So in the case shown above we say:
if its possible and everything is fine, then: send it! ($pmSystem->save();)
otherwise check if there are errors!
PHP Code:
$pmSystem->errors 
if there are errors, then print them.

That is it. You should now be able to automatically send a message with the Vbulletin 4.0.X

What can you do after reading this article?
Have a look at the API
http://members.vbulletin.com/api/vBulletin/vB_DataManager_PM.html

I hope that I was able to help at least one person! Because it took me like three hours to figure this out!!! Enjoy, maybe you could write for what you are using automatic PMs

All the best,
Bjoern aka bpr

Similar Mods

Miscellaneous Hacks Send Message Auto Reply vBulletin 3.8 Add-ons

vblts.ru supports vBulletin®, 2022-2024