sendMsg: Sending 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).

49 Responses to “sendMsg: Sending messages using PHP”


  1. 1 Yusuf

    I always get following error:

    Invalid password and/or password.

    However if I try online demo at sandbaox, it works perfectly.

    Is there any update which was missed in sendMsg.zip file? I also tried update MSNAuth class also (version 1.1.2)

  2. 2 Ramon Smits

    The download link for sendMsg.zip was missing. THe link is : http://downloads.fanatic.net.nz/dev/php/sendMsg.zip

    Other downloads at:
    http://downloads.fanatic.net.nz/dev/php/

    Hope this helps some who got here through Google :)

  3. 3 Daniel

    Thanks Ramon. It used to show at the bottom with WordPress’s meta feature, but I’ve just put up a new theme and it doesn’t display the meta. Will have to modify accordingly.

  4. 4 dave

    The script linked to in comments makes a connection and then goes into a receive loop. Any info on how to actually initiate (send) a message, like your sandbox script does? Can we see some source code for the actual sandbox script?

  5. 5 dave

    My mistake; works fine - thanks!

  6. 6 dave

    I can not get this to work on my server - everytime the form is submitted, i just get a blank page and the following line in the error logs on the server:

    [12-Jan-2007 11:35:18] PHP Warning: fsockopen(): unable to connect to messenger.hotmail.com:1863 in /home/instants/public_html/imtest/sendMsg.php on line 35

    Any ideas? TIA.

  7. 7 Daniel

    That’s usually because the server is blocking the connection, see this post about troubleshooting phpListGrab (which uses the same code).

  8. 8 Vinu

    Works pretty well for us.
    Thanks Daniel.
    Vinu

  9. 9 Kenneth Ma

    this works perfectly, but one thing:

    it fails to send any messages when the account recieves any offline messages.

    it simply just hangs there and stops responding.

  10. 10 Michael

    I’m using this with PHP 4.4.4-8 and it keeps giving me invalid password even though it isn’t invalid. Any ideas?

  11. 11 jack mosley

    i get a PHP Notice: Undefined index: MSNPauth_Key in E:\msnpauth.php on line 39

    help? i have faith in this script i think its an error on my part! how can I fix it?

  12. 12 gokhan

    [quote comment="23169"]i get a PHP Notice: Undefined index: MSNPauth_Key in E:\msnpauth.php on line 39

    help? i have faith in this script i think its an error on my part! how can I fix it?[/quote]

    I’m getting same error message with you.How i can fix it?Online example works perfectly but zipped file doesn’t work :(

  13. 13 Daniel

    Replace msnpauth.php with one of the two available here. The one in the zip on this page is old.

  14. 14 morganusvitus

    The site looks great ! Thanks for all your help ( past, present and future !)

  15. 15 hugo

    is it possible to send mail to all your msn contacts using this, or it should be modified for that kind of use ?

  16. 16 sanjeev kafle

    I found the following error when executing the script.
    I am quite new to php scripting and am a basic learner.
    Please help me.

    Fatal error: Call to undefined function curl_init() in C:\wamp\www\msnmessenger\msnpauth.php on line 26

  17. 17 sebytes

    How can I modify it to send multiple messages or a messages to multiple contacts. Thanks. Sorry but my english is not good.

  18. 18 Ryan

    Howdy. Something’s up right here. On my test system, upon sending a test message nothing happens. All I get is a white page, no code within it.

    In the sandbox, it keeps trying to tell me the user is not signed in…when I am.

  19. 19 Ryan

    Edit: Looked into it a bit. Looks like you’ve tried calling function curl_init() but it’s not defined anywhere within the code, whether this be in the original msnpauth or the the cURL version and the fopensock version just causes my apache error.log to expand to hundreds of megs.

  20. 20 Ryan

    Edit2: Ignore the “user is not logged in” issue: it was just me not adding the test account to my friends list.

    So now, the only issue is this curl_init() function

  21. 21 Ryan

    Edit3: Weird the curl libraries and their dependancies are all set up in PHP. PHP’s set up just fine…so why isn’t it recognising curl_init()?

  22. 22 Ryan

    Edit4: Right, fully sorted the PHP install, basically it meant me copying 2 or 3 dlls to every sys dir on my system…blegh.

    New problem: Now it never recognises my username/password combo. The sandbox works fine, however.

  23. 23 Ryan

    Edit…lots:

    [Tue Jul 03 12:07:56 2007] [error] [client 127.0.0.1] PHP Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://login.passport.com:443 (Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?) in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\msnpauth.php on line 67, referer: http://127.0.0.1:8080/index.php

    This appears in my error.log each time I’m using the fsockopen function. Thankfully, it only does this now, had to modify the code slightly so it wouldn’t inflate the log to 1/2 gig.

    The cURL version (both) simply don’t authorize the username/pass combo. Would this be something to do with a prt forwarding issue?

  24. 24 Breeze

    When will the release of the sandbox version be?

    The current released version does not work… keeps saying my password is incorrect but the sandbox one works

  25. 25 helen statham

    I have just got a new lap top i have been useing msn account and want to use same account can i? The account is in my partners name robinjardine@msn.com

  26. 26 helen statham

    i used msn on my house computer how do i get it on my lap top to send im etc:?

  27. 27 Anton

    Unfortunately no message can be sent when recepient is offline :-/

  28. 28 Herman

    I get the following:

    Notice: Undefined index: SB in /var/www/msn/phplistgrab.zip_FILES/sendMsg/sendMsg.php on line 143

  29. 29 anitha

    its giving error like unexpected varibale

  30. 30 dragontje124

    will there be a msnp 15 version?

  31. 31 Bliss

    I try send message to 3 user, but if first user off-line other user will be off-line too. can you resolve it.

  32. 32 Vindexus

    Nice ****ing class.

  33. 33 Tito

    i don’t understand now this script work or not work

  34. 34 Alexis

    Hi, i tried to use the class but almost always i resive this error:

    Warning: fputs(): supplied argument is not a valid stream resource in /home/xxxx/public_html/sendMsg.php on line 143

    i’m can you help me?

    Regards

  35. 35 Reinaldo Aguiar

    Hi there,

    I think I found a little problem…. If you wish to send consecutive messages to 2 or more users the loop will stop when you find the first offline user.

    The reason is the code on line 48 on file sendMsg.php:

    // Stop if login failed.
    if ($this->result > 1)
    {
    return;
    }

    $this->result will be 217 if the previous user was offline, so it wont start a new session with next consecutive user and te problem will occur in every single step of the loop after the point where the first offline user was found.

    The same problem will occurr with results that are numerically greater than 1.

    Hope this helps,

    Reinaldo

  36. 36 schneid3r

    help

    Fatal error: Maximum execution time of 60 seconds exceeded in C:\xampp\htdocs\new\sendMsg\sendMsg.php on line 120

  37. 37 schneid3r

    $gettotal=intval(@$_POST['totalemail']);
    $start=0;
    $procesados=0;
    $sendMsg = new sendMsg();
    $sendMsg->login($_POST["sender"], $_POST["password"]);

    while($start!=$gettotal)
    {
    $start++;
    $email=stripslashes(@$_POST['email'.$start]);//one email at a time
    $emailss=htmlspecialchars($email,ENT_QUOTES);
    $asunto=”hola $emailss probandooo”;

    if ($email!=”")
    {
    $procesados++;

    //send
    $sendMsg->createSession($emailss);
    $sendMsg->sendMessage($asunto);

    switch ($sendMsg->result)
    {
    case ERR_AUTHENTICATION_FAILED:
    echo $email.” - Login Incorrecto.”;
    break;

    case ERR_SERVER_UNAVAILABLE:
    case ERR_INTERNAL_SERVER_ERROR:
    echo $email.” - Algo fue mal al intentar conectar al server.”;
    break;

    case USR_OFFLINE:
    echo $email.” - El usuario no estaba conectado.”;
    break;

    case OK:
    echo $email.” - Enviado.”;
    break;
    default:
    echo $email.” - Error: “.$phpnickchange->result.”";
    }
    }
    }

    Solo manda al primer correo =(

  38. 38 daklan

    Hello everyone! I want to send message to YM, how can i do it?
    please help me!

  39. 39 daniele

    for send a message ALL contact?? Without writing any Contacts

  40. 40 Carlos

    Hello. this script is very nice but I need to receive answers back. Does anybody have an idea how to receive? thanks

  41. 41 Dave

    Hi!

    can anyone contact me regarding this script? hurripane at gmail dot com

    thanks!

  42. 42 Mike

    it didn’t work for me, but check this out

    http://thinker.rubay.de/archives/20-PHPurple-v0.2-alpha-released.html

    this one works

  43. 43 vladimir

    Nice idea but I get this one

    Warning: fputs(): supplied argument is not a valid stream resource in /home/prelovac/public_html/sendmsn/sendMsg.php on line 147

  44. 44 yingcai.huang

    PHP Notice: Undefined index: SB in /var/www/html/sendMsg.php on line 143
    PHP Warning: fputs(): supplied argument is not a valid stream resource in /var/www/html/sendMsg.php on line 143
    Content-type: text/html
    X-Powered-By: PHP/4.3.9

  45. 45 roy

    I found sendMsg is bug
    Sender password is not long ,I try password is zxcvbnm,.741852963
    command line is error 911
    web message is error Invalid passport and/or password.
    I change password is OK.

  46. 46 sam

    The script tells me that the recipient is offline (err 217) even if it is connected.

    This issue does not occur with some recipients.

  47. 47 Jelle

    Hello,

    Is it possible to send a message to all online persons?

    Thnx,
    Jelle

  48. 48 Platon

    Thanks mate, i find this class very useful :)

  49. 49 jeroen

    Excellent web site I will be visiting often+

  1. 1 Cool Messenger Libraries for Developers « The Storyteller
  2. 2 Send instant message to any MSN contact using PHP « The Storyteller
  3. 3 links for 2007-05-10 « Gevã Schaefer
  4. 4 New version of sendMsg available for testing at Fanatic Live
  5. 5 PHP bots using sockets at Fanatic Live
  6. 6 sendMsg updated again, stable release at Fanatic Live
  7. 7 [vendo] script MSN/Gmail inviti dal sito - AlterVista Forum
  8. 8 [vendo] script MSN/Gmail inviti dal sito - AlterVista Forum
  9. 9 code frenzy » Blog Archive » Send MSN messages programmatically
  10. 10 links for 2008-06-26 | iLenceel

Leave a Reply