用户在Facebook评论框中添加评论时发送电子邮件

问题描述:

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 :)

我刚刚在我的网站上添加了一个Facebook评论框。 我现在想要的是当有人添加评论时触发一些代码“在php中的mail()”。 我希望我的网站向用户发送一封电子邮件,告诉他/她访问者已在您的项目/页面上留下评论。 p>

使用旧评论部分很容易 创建但我现在不知道如何用FB做到这一点。 它甚至可能吗? p>

如果您愿意,请提供详细的答案,如果您不介意,因为我对此并不擅长:) p> div>

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('user@webpage.com','New comment',$msg);
}

Hope that helps!

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.