AmazonIf you use Facebook comment widget on your website don't you wish an email would be sent to you whenever someone leaves a comment in it? Too bad Facebook does NOT offer such functionality. Instead you'll need to use Facebook API and send the email by yourself. Let's look at a sample solution below.
Solution
I use Facebook Javascript SDK API, and I use the following JavaScript code to install the SDK on my website. If you use PHP or other API follow Facebook's tutorial on how to install it. Also I use jQuery. Therefore I put the following code somewhere within $(document).ready(function() {});
window.fbAsyncInit = function() { FB.init({ appId : 'your APP ID', // App ID channelUrl : 'your Channel file', // Channel File; not required status : true, // check login status cookie : true, // enable cookies to allow the server to access the session xfbml : true // parse XFBML }); // Additional initialization code here! For example if you'd like to get an email // whenever someone leaves a comment in the FB comment plugin you need to call // FB.Event.subscribe('comment.create', function(response) {...}); here! FB.Event.subscribe('comment.create', function(response) { url = response.href; //url where the comment is left (e.g. https://one-minute-info.blogspot.com/2012/08/email-when-comment-is-left-in-fb-plugin.html) cId = response.commentID; //ID of the comment generated by FB (e.g. 12951195191605711) sendEmailUrl = 'http://you-email-sending-server/email-sending-service?subject=comment+from+'+url+'+comment+id:+'+cId; iTag = document.createElement("img"); iTag.src = sendEmailUrl; } ); }; // Load the SDK Asynchronously (function(d){ var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(id)) {return;} js = d.createElement('script'); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js"; ref.parentNode.insertBefore(js, ref); }(document));In this solution the key is http://you-email-sending-server/email-sending-service. You need to have a internet ready machine at your disposal that runs the web server. Then you need to run the SMTP server on your machine. If you are using Windows read How to Install SMTP Server on Windows. If you are using Unix read How to Send Emails with PHP on Unix!
Your FB event handling code MUST be placed right following where it says "//Additional initialization code here!" If you place the code somewhere else you might get an error saying the variable FB is defined! This is due to the asynchronous loading of the script.
Suppose you use PHP. Then email-sending-service would be a PHP script which accepts a URL argument called subject. This script would send the value of subject to some email address (which should be your email) via PHP's mail() function. Here's some sample code:
sendEmail($_GET["subject"], 'empty'); function sendEmail($subject, $message){ $headers = "From: someone@some.email.com\r\n" . "Reply-To: recipient@email.com\r\n" . 'X-Mailer: PHP/' . phpversion(); mail('recipient@email.com', $subject, $message, $headers); }recipient@email.com is a placeholder for the email address of the recipient.
Questions? Let me know!