Monday, July 07, 2008

How to send messages using PHP

sendMsg is a class which allows you to send a message to a contact from a specified account. It logs in, changes status to online, creates a new IM session with the target user, sends the message and ends both connections.

The Messenger server imposes a limit on how many IM sessions can be created per minute, so the script is not suited for sending messages to large numbers of contacts within the same login session (see below).

The user sending the message must be on the allow list of the receiving user; or, the target user must have “All others” on their allow list (as in the Windows Live Messenger options dialog). To get the login ticket for authentication it uses the MSNPAuth class, which is included in the ZIP file.

Because of the way the protocol works, the sending user doesn’t appear to come online because it does not synchronize lists.

This class has not been extensively tested, if you find a bug or error, please post a comment here.
Using sendMsg
Standard sending precedure - logging in, creating an IM session and sending a message:
$sendMsg = new sendMsg();
$sendMsg->login('sender@hotmail.com','password');
$sendMsg->createSession('recipient@hotmail.com');
$sendMsg->sendMessage('message', 'Times New Roman', 'FF0000');

The last two arguments for sendMessage() which specify font options are optional. The receiving client’s default font will be displayed if they are not specified.
createSession() should only be called a maximum of 8 times in any 60 second period, due to limits imposed by the Messenger service. The script will throw an error if you try calling createSession a 9th time.
Once createSession() has been called, sendMessage() can be used to send any number of messages to that user, for example:
$sendMsg = new sendMsg();
$sendMsg->login('sender@hotmail.com','password');
$sendMsg->createSession('recipient@hotmail.com');
$sendMsg->sendMessage('this is the first message');
$sendMsg->sendMessage('this is the second message');
$sendMsg->sendMessage('this is the third message');

createSession() can be called for each user that requires a message to be sent to. To send two messages to two different users:
$sendMsg = new sendMsg();
$sendMsg->login('sender@hotmail.com','password');
$sendMsg->createSession('firstperson@hotmail.com');
$sendMsg->sendMessage('hello first person');
$sendMsg->createSession('secondperson@hotmail.com');
$sendMsg->sendMessage('hello second person);

There is also a function which simplifies the procedure of sending a message to a single user:
$sendMsg = new sendMsg();
$sendMsg->simpleSend('sender@hotmail.com','password', 'recipient@hotmail.com','message');

The result property can be used to see how it went, refer to the constant definitions at the top of sendMsg.php for textual meaning. For description of socket errors, you can check the property error.
You can give it a try at the sandbox. If you have troubles getting the script working with connections error, please refer to Troubleshooting phpListGrab (phpListGrab uses similar code).
From: http://www.fanatic.net.nz/2005/02/15/send-a-message-using-php/
 
Blogger Template Layout Design by [ METAMUSE ] : Code Name Gadget 1.1 Power By freecode-frecode.blogger.com & blogger.com Programming Blogs - BlogCatalog Blog Directory