当有人在Facebook评论插件中留言时发送通知电子邮件

时间:2012-01-04 18:05:00

标签: facebook api sdk

当有人在Facebook评论插件中发表评论时,如何发送通知邮件。

我有这个脚本,但每当有人来我的页面时,我都会收到一封电子邮件。

我只想在新用户评论页面时收到电子邮件

<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      = 'info@a2bdjs.com';
 $subject = 'Comment Posted on Testimonial Page';
 $message = 'Comment Posted on Testimonial Page';
 $headers = 'From: info@a2bdjs.com' . "\r\n" .
'Reply-To: info@a2bdjs.com' . "\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>

2 个答案:

答案 0 :(得分:1)

您必须按以下方式拨打电话。

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

然后将以下内容放在上面指定的php文件中。

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

你可以再运行一些检查,以便更安全。

答案 1 :(得分:0)

你有一些奇怪的事情发生在那里。您将服务器端代码(PHP)放在客户端代码(Javascript)中。 PHP代码将在您的服务器上执行,因此您必须将该代码放在单独的文件中并对该文件进行AJAX调用(使用JavaScript),该文件将执行PHP代码并发送邮件。

摆脱FB.Subscribe函数中的PHP代码,改为:

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

然后打开控制台(Chrome中的F12用于开发人员工具,或者使用firefox上的firebug)。 看看response变量,您就可以看到发生了什么类型的事件。