Cork Deejays
Cork Deejays

Reputation: 11

Send a notification email when someone leaves a comment in Facebook comments plugin

How do I send a notifiaction email when someone leave a comment in Facebook comments plugin.

I have this script but any time someone comes to my page I get an email.

I only want to get an email when a new user comments on the page

<script> window.fbAsyncInit = function() {



    FB.init({
      appId      : 'appid', // App ID
      channelUrl : '//http://www.corkdiscos.com/channel.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });


FB.subscribe('comment.create', function(response){
  <?php   
 $to      = '[email protected]';
 $subject = 'Comment Posted on Testimonial Page';
 $message = 'Comment Posted on Testimonial Page';
 $headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();   
mail($to, $subject, $message, $headers);
?>
});

};


  // 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>

Upvotes: 1

Views: 1630

Answers (2)

Joshua Abenazer
Joshua Abenazer

Reputation: 146

You would have to make an ajax call in the following manner.

/* Get FB comment notification */
<script>
    $(window).load(function(){
        FB.Event.subscribe('comment.create', function(response) {
            var data = {
                action: 'fb_comment',
                url: response
            };

            $.post( '`URL TO THE PHP FILE CONTAINING THE MAIL CODE`', data );
        });
    });
</script>

And then put the following in the above specified php file.

<?php   
 if ( isset( $_POST['url'] ) ) {
     $to      = '[email protected]';
     $subject = 'Comment Posted on Testimonial Page';
     $message = 'Comment Posted on Testimonial Page';
     $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion();   
     mail($to, $subject, $message, $headers);
}
?>

You could run a few more checks to be on the safer side.

Upvotes: 1

Lix
Lix

Reputation: 47976

You have something weird going on there. You are placing server-side code (PHP) inside client-side code(Javascript) . The PHP code will be executed on your server, so you'll have to place that code in a separate file and make an AJAX call (with JavaScript), to that file, which will execute the PHP code and send the mail.

Get rid of that PHP code inside the FB.Subscribe function, put this instead :

FB.subscribe('comment.create', function(response){
    if(typeof console != 'undefined') {
        console.log(response);
    }
});

Then open up the console (F12 in Chrome for the developers tools, or with firebug on firefox). Take a look at the response variable and you'll be able to see what type of event has happened.

Upvotes: 0

Related Questions