Sunday, July 25, 2010

Perl - send email without using sendmail

In a recent exercise, I was required to write a script which would attempt sending email through an smtp server instead of sendmail. At a periodic interval, the script would check if the smtp server is able to send emails or not. Basically, this script ran as a background process.

#!/usr/bin/perl

use Net::SMTP;
use POSIX qw/strftime/;
if (@ARGV != 5)
{
  usage(); 
  exit;              
}

my $smtpmailhost = $ARGV[0];
my $from_address = $ARGV[1];
my $to_address = $ARGV[2];
my $timeout = $ARGV[3];
my $interval = $ARGV[4];

my $logdir = "/tmp/email_logs/";

my $datestamp = strftime('%m-%d-%y_%H:%M:%S',localtime);
$mday = (localtime(time))[3];

my $logfile = $logdir."$smtpmailhost\_email_log\_$datestamp.log";
open LOG, ">>","$logfile" or die $!;
LOG->autoflush(1);
while(1) {
    $smtp = Net::SMTP->new($smtpmailhost,
                            Timeout => $timeout);
    $cur_datestamp = strftime('%m-%d-%y_%H:%M:%S',localtime);
    $cur_mday = (localtime(time))[3];
    if(!$smtp) {
        print LOG "\n$cur_datestamp : Error creating smtp object for $smtpmailhost: $!";
        sleep $interval;
        next;               
    }       
    if($cur_mday != $mday) {
        close(LOG);
        $logfile = $logdir."$smtpmailhost\_email_log\_$cur_datestamp.log";
        open LOG, ">>","$logfile" or die $!;
        LOG->autoflush(1);
        $mday = $cur_mday;
    }   
    $smtp->mail($from_address);
    $smtp->to($to_address);
    $smtp->data();
    $smtp->datasend("Subject: $smtpmailhost - Test email message\n");
    $smtp->datasend("\n");
    $smtp->datasend("Test email has been sent successfully at $cur_datestamp!");
    $smtp->dataend();
    $smtp->quit;
    print LOG "\n$cur_datestamp : Test Email sent via $smtpmailhost!";   
     sleep $interval;   
}

close(LOG);
sub usage
{
  print "Invalid parameters \nUsage: ./smtpemail_send.pl \n";
}




Please let me know, of any corrections/improvements.

Sunday, June 20, 2010

Yii + MySql + Wamp







First, I installed WampServer, which is a package of Apache, Php and MySql coupled into one. In the httpd.conf file I pointed the “DocumentRoot” and “Directory” configurations to the directory, where I would have my application and Yii framework.


I copied the Yii framwork in "d:/workspace/app_demo/”

To create the stub application from yii, I executed these commands for my setup:

>cd D:\workspace\app_demo

>yii\framework\yiic webapp myapp

-------

This created an application “myapp” under “app_demo” folder.

The “/app_demo/myapp/protected/config/main.php” file contains the database configurations. By default the database was pointed to an sqlite database. I changed that, to point to Mysql database. So I had these settings for my mysql database.

/* 'db'=>array(

'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',

),*/

'db'=>array(

'connectionString' => 'mysql:host=localhost;dbname=pages_db',

'emulatePrepare' => true,

'username' => 'root',

'password' => '',

'charset' => 'utf8',

),

I also enabled logging through this file to look like this:

'log'=>array(

'class'=>'CLogRouter',

'routes'=>array( array('class'=>'CFileLogRoute',

'levels'=>'error, warning',

),

array('class'=>'CWebLogRoute',

), ), ),

-------

My database had two tables (user and address). The sql is shown below:

CREATE TABLE IF NOT EXISTS `address` (

`addressid` int(11) NOT NULL AUTO_INCREMENT,

`firstline` varchar(45) DEFAULT NULL,

`secondline` varchar(45) DEFAULT NULL,

`city` varchar(45) DEFAULT NULL,

`state` varchar(45) DEFAULT NULL,

`country` varchar(45) DEFAULT NULL,

PRIMARY KEY (`addressid`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `user` (

`userid` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'This contains the userid of the user',

`fname` varchar(128) NOT NULL COMMENT 'This contains the first name of the user',

`lname` varchar(128) DEFAULT NULL COMMENT 'This contains the last name of the user',

`sex` varchar(1) NOT NULL COMMENT 'This contains the sex of the user',

`age` int(100) DEFAULT NULL COMMENT 'This contains the age of the user',

`creation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'This contains the user account creation',

`user_addressid` int(11) DEFAULT NULL,

PRIMARY KEY (`userid`),

KEY `fk_user_address` (`user_addressid`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `user` ADD CONSTRAINT `fk_user_address` FOREIGN KEY

(`user_addressid`) REFERENCES `address` (`addressid`) ON DELETE NO ACTION ON

UPDATE NO ACTION;

-------

I had to create corresponding models for the tables. After the database configuration, I ran the following commands to create the models in my app.

cd app_demo\myapp

D:\workspace\app_demo\myapp>protected\yiic shell index.php

This opened the Yii Interactive Tool.

Yii Interactive Tool v1.1 (based on Yii v1.1.2)

Please type 'help' for help. Type 'exit' to quit.

>> model *

The following model classes (tables) match your criteria:

1. Address (address)

2. User (user)

Do you want to generate the above classes? [Yes|No] yes

generate models/Address.php

generate fixtures/address.php

generate unit/AddressTest.php

generate models/User.php

generate fixtures/user.php

generate unit/UserTest.php

The following model classes are successfully generated:

Address, User

If you have a 'db' database connection, you can test these models now with:

$model=User::model()->find();

print_r($model);

-------

After this, I edited the actionIndex function in SiteController.php to look like this:

public function actionIndex()

{

// renders the view file 'protected/views/site/index.php'

// using the default layout 'protected/views/layouts/main.php'

$this->render('index', array(

'user' => User::model()->findAll()

));

}

In the index.php, I added the following table to display the records from the database:

 
foreach($user AS $us):?> 
 echo $us->fname;?> 
echo $us->lname;?> 
echo $us->age;?> 
echo $us->user_address->city;?> 
 endforeach;?>

Then I pointed my browser to http://localhost/myapp/index.php and this generated a table with the results.

More on this later...

Sunday, April 11, 2010

Spidvid: Let's create better videos!


The start-up scene is always sprawling with action. Individuals keep coming up with brilliant ideas to tackle a problem at hand. If you ever heard of Youtube, you can relate to the fact, that the online video sharing platform is filled with low quality videos. Enter the new service round the corner - Spidvid. It is a platform which brings together video creators and professionals, who would ultimately produce quality entertaining videos. It doesn't end here; they go further, distribute the videos and generate compensation for their contribution to the effort.

It is an inventive concept accented by a beautiful user interface. It is easy to use and very responsive. You can choose any role you want on the video creation life cycle. You may be a video creator, a professional who can act, write, edit or even an amateur who has video ideas which, you would like to be created by others. This way you get in touch with people with similar interests and learn from their experiences.

Spidvid will raise the bar for online videos. It brings together people from different corners of the planet, with a common goal of producing quality videos. Right now, Spidvid is offered as a beta service. As more and more users join the bandwagon, the talent pool will get richer, as you have more contributors.

Getting content onto the website is a task which the marketing team needs to pay attention to. I would like to have a section to see some clips of the videos created on Spidvid.

Overall, it is a good beginning to a new frontier in video collaboration. Spidvid has given users the tools which they need to connect and develop videos outside a single fortified studio. By making it open to public, the content will cross the geographical boundaries and be enjoyed by a wide range of viewers.

So, let's get started and create some videos!

Tuesday, October 20, 2009

Windows - the Seventh

“And the SEVENTH angel poured out his vial into the air; and there came a great voice out of the temple of heaven, from the throne saying, It is DONE.” (Revelation 16 : 17)

The number 'Seven' has a lot of significance. There are seven days in a week and it was on the seventh day that the world was completed. 'Seven' symbolizes completeness or perfection. Perfection is only divine. At an earthly level, I would attribute seven to refinement. In that respect, Microsoft has achieved elegance with W7. A few things I like about W7 are:
1. The annoying UAC has become much more placid. Introduced in Vista, as a security measure, it turned out to irritate the users with its frequent pop-ups.
2. The boot performance is significantly faster. Windows 7 should have achieved a 15-sec boot up by the time it is up for final release.
3. W7 lets the users connect to wireless networks instantly with fewer clicks which will prove to be of lot of help, for users without much technical know-how.
4. The footprint of the OS is much lighter compared to Vista. It installs faster and occupies lesser space on the hard disk. Some of additional software like Windows Mail have been made optional downloads instead of default installations.
5. The new OS uses lesser battery power and is known to save energy by 20%. That is a very good news for netbook makers as, Windows 7 will prove to be the OS for netbooks, too.
6. There are some cool mouse gestures included: Aero Peek and Aero Shake. Aero Peek makes all the open windows transparent and lets the users peek through their desktop revealing hidden gadgets and icons while, Aero Shake lets the users minimize unnecessary windows from the desktop by shaking the required window.
7. The OS supports backward compatibility. Legacy applications can run in a Windows XP mode, inside of Window 7.

And, something I don't like about W7:
1. Microsoft failed to come up with a suitable strategy for integrating the cloud. With computing being the talk of the town, and Microsoft itself working on Azure, the Software as a Service(Saas) integration seems to have been lost in translation. Microsoft could have amalgamated its Live Mesh with Windows 7 for data backup.
2. W7 still lives on to the concept of one partition OS. I would have liked this to change. The data like Documents, Music, Pictures and other user settings should reside on a different partition than the OS itself. This would be of great value in case of a system crash or an OS reinstall.

Overall, W7 is a nice compact mixture of niceties and removes some of the nuisances of Vista.

Friday, July 3, 2009

Opera innovations

When I read about Opera's "Reinvent the Web" campaign on Techcrunch, I was a bit excited and a bit cynical about it's legitimacy. Similar tall claims have been made earlier but with little success. Initially, I thought it was a mere marketing gimmick but, Opera did excite me on June 16, 2009. Some of the innovative features by Opera are:

1. Appearance: The Opera browser has a very polished look. It is very much a Windows 7 product. Firefox looks too XPed, IE never excited me and I have never used an Apple.

2. Mouse Gestures: One of the first things I tried to experiment was mouse gestures. Hold your right mouse in the tab and flick it to left, it performs the "Back" functionality of the browser.

3. Speed Dial: This is such a handy feature which has been there on mobile phones for years, but nobody thought of incorporating it in the browser.

4. Closed tabs: If you close any of your tabs accidentally, Opera lets you open them instantly. More important is the accessibility of this button. It is right there where I would want it to be.

5. Unite: This is something which has a potential to change the way the web works. It allows you to share files from your system rather than some third party service. By giving the user, control of his information, Opera makes the user, the master of his information. But I feel it is little too early for something like this to become an instant hit. Poor Internet speeds, limited hardware resources would be some key limitations for Unite's success. Nevertheless the idea deserves credit.

6. Widgets: These are web applications which run in the browser but without showing the browser controls. They are very easy to use and also very easy to build using client side programming languages. I have been playing around with them and will write more about how to create your own widgets, but that is later.

7. Anti Cloud: Yes Opera is anti-cloud. The world has been focusing on centralizing services across the Internet for quite some time now. With a few big names in the fray, the race has heated up. But, Opera has different plans. By providing an individual to host his own web server inside a browser, Opera has de-centralized information.

Tuesday, June 23, 2009

Cross domain calls

The XMLHTTPRequest object is used to make an http request directly to a web server and load the server response data back into the scripting language. But this techniques works only when you are making a request to the same web server with in the same domain. Here is how to make a call from www.site1.com/user.html to www.site2.com/usercheck.aspx :

1. Create a web page on www.site1.com which would make a call to another server www.site2.com. Documentation available inline.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<title>Check username availability</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter Username:
<!-- Check if username exists already or not -->
<input type="text" name="user" onchange="createScriptTag(this); return false;"/>
<span id="outputmessage"></span>
<input id='submit_button' name='submit_button' type='submit' value='submit' />
</div>
</form>

<script language="javascript" type="text/javascript">


//Create a script tag which loads javascript from a remote source
function createScriptTag(e){
var headElement;
var scriptTag;
var inputValue = e.value;
var url = 'http://www.site2.com/usercheck.aspx?username=' + inputValue + '&rnd=' + new Date().getTime();
headElement = document.getElementsByTagName("head").item(0);
scriptTag = document.createElement("script");
scriptTag.setAttribute("id", "user_script");
scriptTag.setAttribute("type", "text/javascript");
scriptTag.setAttribute("src", url);
headElement.appendChild(scriptTag);

// Creates issue with IE so remove the script tag later at an appropriate place
//headElement.removeChild(scriptTag);

}

//function to be executed on callback from the above request. The callback returns a response similar to "isAvailable({'status':1})"
function isAvailable(response){
if (response.status == 1){
document.getElementById("outputmessage").innerHTML = "Username is still available";
}
else {
document.getElementById("outputmessage").innerHTML = "Username already exists";
}
}

</script>
</body>
</html>

2. The www.site2.com/usercheck.aspx page accepts querystring parameters, 
implements a business logic and returns the function to be executed on client side as response.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class keygenerator : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/plain";
if (Request.QueryString.Count > 0)
{
string username = Request.QueryString[0];

//array of names to check against
ArrayList arlExistingUsernames = new ArrayList();
arlExistingUsernames.Add("harpreet");
arlExistingUsernames.Add("singh");

if (!arlExistingUsernames.Contains(username))
{
Response.Write("isAvailable({'status':1})");
}

else
{
Response.Write("isAvailable({'status':0})");
}
}
else
{
Response.Write("isAvailable({'status':0})");
}
Response.End();
}
}

For more cross domain calling options check out:

http://snook.ca/archives/javascript/cross_domain_aj/

http://ox.no/posts/ajast-cross-domain-rest-calls-using-json-injection

http://developer.yahoo.com/javascript/howto-proxy.html


Technorati Tags: ,,

Saturday, June 20, 2009

Turbogears debugging in Eclipse

Setting up debug enabled environment for a Turbogears application in Eclipse is not really straightforward. I struggled a lot before I could set it up. Initially I wasnt even sure if I could set up break points and debug the application like in microsoft visual studio. It is very much possible although some of the things are not very obvious. Here is a list of things to check up, if you are facing problems.

1. You can install Pydev by following the instructions mentioned on pydev website. You can also have a look at this ibm site which helps you make the necessary settings in Eclipse.

2. You can patch the Decorators file from pydev blog. If you are facing any problems drop your email id I will send you the patched file.

3. Pydev gives problems with certain turbojson package versions. You may try to downgrade and install a previous version of turbojson package. Use the following command to do so:
>easy_install -U turbojson==1.1.4

>easy_install -U DecoratorTools==1.4 

4. Remember, you will still see the following error when you start the debug session.

PYDEV DEBUGGER WARNING:

sys.settrace() should not be used when the debugger is being used.

This may cause the debugger to stop working correctly.

If this is needed, please check: 

http://pydev.blogspot.com/2007/06/why-cant-pydev-debugger-work-with.html

to see how to restore the debug tracing back correctly.

Call Location:

  File "C:\Python24\lib\site-packages\decoratortools-1.4-py2.4.egg\peak\util\decorators.py", line 562, in uninstall


But, wait for a few seconds and eclipse will start the debug session after the error message.