Mohammed A. Al Jama
Mohammed A. Al Jama

Reputation: 511

sending email when user adds a comment on facebook comment box

I added a Facebook comment box to my website a while ago. What I want now is to trigger some code "mail() in php" when someone adds a comment. I want my website to send an email to the user telling him/her that a visitor has left a comment on your item/page.

It was easy to do with the old comment section I created but I don't know now how to do it with FB. Is it even possible?

If yes please include a detailed answer if you don't mind since I am not that good at this :)

Upvotes: 1

Views: 1261

Answers (2)

Anonymous
Anonymous

Reputation: 3689

If you are not happy with the solution FB already provides (as scjosh explained), you can use Facebooks Javascript SDK a bit of ajax (jquery would be my favorite) to load a script with the mail() function.

First load the JS SDK (make sure you change 'YOUR_APP_ID' and the channelURL!!):

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'YOUR_APP_ID', // App ID
      channelURL : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      oauth      : true, // enable OAuth 2.0
      xfbml      : true  // parse XFBML
    });

    // Additional initialization code here
  };

  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));
</script>

more information about js sdk, the loading and the channel file can be found here. You should have an app or create an app in facebook for that reason. Information about setting up an app is here.

Next step is to load the script with the mail() per jquery:

FB.Event.subscribe('edge.create',
    function(response) {
        $(document).load('mail.php?response='+response);
    }
);

And finally the mail.php:

if(isset($_GET['response'])) {
$msg = 'A comment was left on '.$_GET['response'];
mail('[email protected]','New comment',$msg);
}

Hope that helps!

Upvotes: 2

Josh Ferrara
Josh Ferrara

Reputation: 767

If I understand you correctly, Facebook already includes integrated email communications via their website. I.E., if the Facebook user has notifications enabled for your comment threads, an email will be sent to that user upon request. The only other way to make this possible would be to hack together your own comment box and hook it with the Facebook API.

Upvotes: 2

Related Questions