Follow this Blog!

Aug 24, 2012

f Comment

Email Me When Someone Leaves a Comment in the Facebook Comment Plugin!

If you would like an email notification sent to you whenever someone leaves an FB comment on your website let me know and I'll set up this service for you for free.
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.

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() {
   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!
    function(response) {
      url = response.href; //url where the comment is left (e.g.
      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
  var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
  if (d.getElementById(id)) {return;}
  js = d.createElement('script'); = id; js.async = true;
  js.src = "//";
  ref.parentNode.insertBefore(js, ref);
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:\r\n" .
     "Reply-To:\r\n" .
     'X-Mailer: PHP/' . phpversion();
  mail('', $subject, $message, $headers);
} is a placeholder for the email address of the recipient.

Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
Find Michael on Google or Facebook
ADVERTISING WITH US - Direct your advertising requests to Michael