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).
Somehow I suspect that this will be a long post…
First I would like to respond to a few of the other comments about receiving 217 even though the contact is online.
The receiving contact must have the sender on their contact list to guarantee to work, OR make sure that “Only people on my Allow List can see my status and send me messages” on Options > Privacy in WLM is disabled. I’d recommend just adding the contact (manually, because this script doesn’t have the functionality to add contacts).
I have been using this class in the project I have been working on over the past year or so, and randomly I kept receiving fputs argument is not a valid stream resource. As the script is in heavy usage, I got a lot of these errors. So finally I added some debugging statements to log all of the protocol messages to file to see where things were breaking. Turns out that MS is adding some unexpected text into the protocol, and the script can’t find what its looking for.
In an ideal situation, here is what is sent in the script
After sending from the script:
<<< NS CHG 4 NLN
<<>> NS CHG 4 NLN 0
>>> NS XFR 5 SB 207.46.26.159:1863 CKI 354526828.16034168.884359
The switchboard session is opened, and messages can be sent.
Now, what I discovered is the response to these can get garbled:
>>> NS NOT 340
>>> NS To see your offline messages, get the latest version of Windows Live(TM) Messenger.XFR 5 SB 64.4.37.58:1863 CKI 528727238.18922235.378208
>>> NS CHG 4 NLN 0
What happened here is a NOT is sent from the server (in this case, string length of 340) with a notification sent on the following line. Immediately following the Notification, on THE SAME LINE, was either the responding CHG or XFR from the NS. The result in the code is the _process_data function does not find the XFR and sits idle until timing out.
Here’s my solution. In the _process_data function’s switch statement, add in a new case. Most of the code is just repeated from _read, except the notification string is truncated. Hopefully this code won’t look horrible after posting
case ‘NOT’:
$data = substr(fgets($this->_sockets['NS'], 1024), $params[1], -2);
$r = $this->_process_data($data);
while (!feof($this->_sockets['NS']) && !$r)
{
$data = fgets($this->_sockets[$socket], 1024);
if (!$data)
{
continue;
}
$data = substr($data, 0, -2);
$r = $this->_process_data($data);
if ($r)
{
return;
}
}
return(true);
break;
Some updates to the last post, it didn’t format properly:
After sending from the script:
<<< NS CHG 4 NLN
<<< NS XFR 5 SB
>>> NS CHG 4 NLN 0
>>> NS XFR 5 SB 207.46.26.159:1863 CKI 354526828.16034168.884359
The switchboard session is opened, and messages can be sent.
Now, what I discovered is the response to these can get garbled:
>>> NS NOT 340
>>> NS <NOTIFICATION ver=”2″ id=”2″ siteid=”0″ siteurl=”http://g.live.com/”><TO name=”email at domain.com” pid=”0×0:0×0″/><MSG pri=”1″ id=”2″><ACTION url=”5meen_ca/80″/><SUBSCR url=”5meen_ca/80″/><BODY lang=”1033″ icon=”"><TEXT>To see your offline messages, get the latest version of Windows Live(TM) Messenger.</TEXT></BODY></MSG></NOTIFICATION>XFR 5 SB 64.4.37.58:1863 CKI 528727238.18922235.378208
>>> NS CHG 4 NLN 0
How can I modify it to send multiple messages or a messages to multiple contacts.
Recieving Error (217) The user appears to be offline. Even if the user is online. Can IM will only be sent to online users and the contacts that are in the contact list………….
Hi… your script work for me well.. but i need to send msg to offline contact as well… is that possible…?
I have searched in google, but unable to get it.
waiting for your reply… Please reply me soon…
this script seems all find and dandy but we can NOT send to Offline users. Based on the above messages, this would seem to be a Number One Problem with the script.
Please e-mail me with the solution.
Nelson buck
Hi,
How to send msn messages to 2 users. it should send to both users even though the first user offline. any idea on this