2012-01-04 146 views
1

如果有人在Facebook评论插件中留言,我该如何发送通知邮件。当有人在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  = '[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> 

回答

0

上一个新的用户评论您有什么奇怪的事情那里收到一封电子邮件。您将服务器端代码(PHP)放在客户端代码(Javascript)中。 PHP代码将在您的服务器上执行,因此您必须将该代码放在单独的文件中,并对该文件执行AJAX调用(使用JavaScript),该文件将执行PHP代码并发送邮件。

摆脱FB.Subscribe函数内部的PHP代码,把这个改为:

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

然后打开控制台(F12在Chrome的开发者工具,或在Firefox萤火虫)。 查看response变量,您将能够看到发生了什么类型的事件。

+0

iv使用上面的代码更新了我的网页。 http://corkdiscos.com/testimonials.html我在找什么console.log? – 2012-01-04 20:45:39

1

您将不得不按以下方式进行ajax调用。

/* 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  = '[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); 
} 
?> 

您可以运行一些更安全的检查。

相关问题