Sending out emails from Seaside/Squeak

I think something that probably no-one has covered so far in any Seaside blogs is how to send SMTP emails through Seaside or Squeak. If you’re doing a public Seaside application you’ll probably end up having to send emails for various reasons like resetting of passwords or alerts to users etc, so this is something useful to have up your sleeves.

There are a number of ways to send an email in Squeak and Seaside and I know of 3 at the moment.

1. There is a utility class, which contains only class-level messages, called SeasidePlatformSupport, and it has a message called ‘deliverMailFrom: to: text: ‘ that sends a mail directly. However, you’ll need to set the SMTP server first, by using MailSender.

This is a code snippet you can cut and paste directly on to a workspace to evaluate(though obiviously replacing the appropriate email addresses):

MailSender setSmtpServer.
SeasidePlatformSupport deliverMailFrom: '<fromEmail>'
to: #('<toEmail>')
text: 'Subject: Hello from SeasidePlatformSupport!
Sending from SeasidePlatformSupport'.

Don’t worry that the setSmtpServer message doesn’t take in any parameters. What happens is that MailSender pops up a dialog box for you to enter the SMTP server. But you’ll need to enter an SMTP server that doesn’t need authentication because there isn’t a way to set the user name or password from this mechanism. Of course you can’t set the mail server from Seaside either (the dialog box is Morphic) but you can run the MailSender once from the Squeak environment and you’ll be good for the rest of the application. Notice the ‘to’ is actually an array of recipients.

2. The second way is to go directly to the MailComposition class that SeasidePlatformSupport is using (through MailSender). This is another piece of code snippet that you can directly evaluate from workspace:

MailSender setSmtpServer.
msg := (MailMessage new setField: 'to' to: '<toEmail>';
setField: 'from' to: '<fromEmail>';
setField: 'subject' to: 'Hello from MailMessage!';
body: 'Sending from MailMessage'; yourself).
(MailComposition new sendMailMessage: msg) submit.

Again you’ll need to set the MailSender SMTP server first. There isn’t any much difference in doing it this way, as far as I can see, but this is another way you can send emails.

3. Another way is directly go to the underlying guts of the other two mechanisms and use the SMTPClient class itself to send the email. SMTPClient subclasses TelnetProtocolClient and if you dig a bit deeper you’ll realise that it does nothing but to open up a socket session at the appropriate port and send in the SMTP commands as text.

username:= '<aUsername>'.
password:='<aPassword>'.
client := SMTPClient openOnHostNamed: '<yourSmtpServer>' port: <portNumber>.
client user: username; password: password; login; initiateSession.
client mailFrom: '<fromEmail>';
 	recipient: '<toEmail>';
 	data:
'Subject: Hello from SMTPClient!
From: <fromEmail>
To: <toEmail>
Sending from SMTPClient!'.
client quit.

This is actually the underlying code that sends the mail in the first two mechanisms though they are couched by different classes. Of course ultimately you can use TelnetProtocolClient itself to do the work but that’s a bit going down to the basics too much :)

Note that this last method of sending emails you can authenticate the SMTP server and also set the port number.

Seaside programmers in Singapore?

I’ve been fiddling around with Seaside for a while now and I was wondering if there is anyone in Singapore (besides myself) doing it. Would be interesting to hear from someone, anyone, either a beginner like myself or a veteran Smalltalker perhaps? It would be nice to talk shop to someone nearer at home. I think there is probably too little current interest to start up Seaside community in Singapore but if there is anyone interested in exploring or learning this together do drop me a note. Would love to hear from you.

A simple introduction to Seaside

I took about a month off technical postings in this blog mostly because of the many trips I made over the past 2 months but I’m back again! Let me start the new year by coming back to a topic that seems (surprisingly) popular amongst my blog readers — Seaside on Squeak.

The last I left off, I was explaining how the Squeak environment works. I’ll not talk more about Squeak this time round and focus on Seaside but you should know that this posting and most likely all my postings on Seaside are based on Squeak. I tried Cincom Smalltalk for a short while, but my personal preference is still Squeak, probably because I started with it first.

In this post, my focus is on getting you to run Seaside the fastest way possible, so I’ll drop out most of the features and comparison discussions for another time.

You can find most if not all Seaside materials, downloads and links in the Seaside main site (http://www.seaside.st). Assuming you already have Squeak running (if not please refer to my earlier post), you should download this image (http://www.seaside.st/Download/Images/). There are other images with more features or tighter configurations though, if you dig around the Seaside main site or Google for it.
Load this image. You should get something like this:

Seaside startup

In the ‘Welcome to Seaside’ window you should see some instructions and a simple explanation on how to get started. What always tickled me pink on is that this apparently ‘readme’ file is also a point for executing Seaside! It took me a couple of minutes (when I first encountered this) to realise that to start up Seaside you just have to select the ‘WAKom startOn: 8080.’ line in the window, right-click and select the ‘do-it’ action on the menu (the one with the lightning icon). That’s it! You’ve just started Seaside.

Go back to your normal browser, go to ‘http://localhost:8080/seaside/counter&#8217; and you should see the application running. What is also interesting is that if you save this image before you exit Squeak (and therefore Seaside), the next time you start Squeak with this image, Seaside starts automatically. It seems pretty obivious if you think about it, but it’s one of those nice little things that normally don’t get with other environments.

Login into the Seaside configuration application and muck around with that a bit. You should see a list of applications that have already been installed as samples. One of the more interesting samples is the Sushi store. You should browse around that one to see how things work.

By now you’ll notice the Seaside toolbar at the bottom of each web application. This toolbar is another interesting feature in Seaside web applications. It’s there in all applications and you can use it to configure, test or even program the application right there and then!

Click on the Toggle Halos link. You’ll a box surrounding each component on the web page. Each component has 3 icons — System Browser, Inspector and Style Editor. The Style Editor allows you to change the look and feel of the application using CSS styles. The Inspector allows you to look into each component in details while the System Browser is very much like the System Browser in Squeak and allows you to change the behavior of the component itself.

If you want to disable the Seaside toolbar, click on ‘Configure’ in the toolbar. Under the ‘General’ heading you will see a ‘Deployment Mode’ option, which is set to ‘false’ by default (the value is inherited from WAGlobalConfiguration). Click on the ‘override’ link and this will give you an option to change it to ‘true’. Do that, then scroll to the bottom of the web page and save your configuration, then click on ‘done’ to exit the configuration application and the toolbar should go away. If it doesn’t, just remove the session tied to the URL at your browser or refresh the page.

This is a very simple whirlwind introduction to running Seaside. The next post on Seaside I’ll go into writing a simple web application. If you are eager to know more and can’t stand my rambling style, try David Shaffer’s excellent tutorial (http://www.shaffer-consulting.com/david/Seaside/) or the famous A Walk on the Seaside tutorial (http://www.seaside.st/Tutorial/AWalkontheSeaside/). Another great resource is Stephane Ducasse’s videos (http://www.iam.unibe.ch/~ducasse/Videos/ )

A primer on the Squeak programming environment

Before we start actually doing anything on Seaside, it’s important to learn a bit on the environment. I’m working on the assumption that, like myself, you know next to nothing about Squeak or Smalltalk. The first thing you need to do is to download the environment from the Squeak website. You should know that Squeak consists of a ‘virtual machine’ that is specific to a platform, and an image (usually .image file). The image file is rather large (usually in tens of MB) but this should not be shocking as it is the complete environment.

Once you have downloaded your VM of choice (from now on, I’ll use Windows since I run Windows most of the time) and an image of choice (I use the one from Seaside), you’re ready to go. Double click on squeak.exe and select your image.

squeak_env.png

Once you get into the environment you’ll see a nice graphical user interface which is quite familiar but yet not familiar. To start an application, you don’t double click but drag and drop it on to the main workspace. There is an application called Workspace, which is ‘console-like’. You can type Smaltalk in it, then right-click and select ‘do it’ to run your script. The shortcut on Windows is use alt-d. For example, to run the the Komanche web server, type ‘WAKom startOn: 8080’, select this line and ‘do it’.

One of the first things you’ll see is the System Browser. You might think it strange but it’s really going to be your main development tool. There are five main panes, 4 of them in the top row and a big one at the bottom row. If you don’t see it, left click once on the desktop (to get the ‘world’ popup menu), select ‘open …’ and then ‘browser’.

squeak_sys_browser.png

Starting from the left, the first pane is the class category pane. Class categories are convenient placeholders for classes, a means of organizing your classes. To add a new class category, right click once and select ‘add item…’. Fill up the input box when it pops up and you have a new class category.

The second pane is the class pane. This pane shows a list of classes for the class category you select. To add a new class, if you expect to right-click the class pane and select ‘add class’ you’ll be dead wrong. I was stumped for a few days on this. Notice if you create a class category ‘Saush’ on the first pane and select it, you will see something like this at the bottom pane:

Object subclass: #NameOfSubclass
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Saush'

This is the class template. Change #NameOfSubclass to something else e.g. #MyFirstClass (remember to keep the hash), then right click and select ‘accept’ or alt-s to save. You will see a new class appearing at the second pane called ‘MyFirstClass’. At the bottom of the second pane are two buttons, labeled ‘instance’ and ‘class’ respectively. If you click on the instance button you’ll show the instance method categories and methods to the third and fourth panes. If you click on the class button, you’ll show the class method categories and class methods on those panes.

The third pane is the method category pane. Method categories are just means of classifing methods and have no other meaning than that. How do you add a new method category? If you guessed right clicking on the pane and selecting ‘new category’, then you’re absolutely correct! There is a method category called ‘all’ that you can click and display all methods on the right-most pane.

The fourth and last pane to the right is the method pane. If you have followed the above carefully you might have guessed that to create a method belonging to a method category you’ll select the method category, then fill up the template displayed at the bottom pane and save it. The template looks something like this:

message selector and argument names
"comment stating purpose of message"

| temporary variable names |
statements

‘Message’ here means the class method (Smalltalk calls all methods “messages”). Comments are delimited by the ” (whereas most languages use ” for string definition. Whatever you define between the vertical bars ‘|’ are variables you will use in the method. Note that if you create a method when selecting the ‘all’ method category you’ll get a ‘as yet unclassified’ category and your new method will belong to it. To change the classification of the method just drag and drop it to the correct method category.

One of the menu items when you select from any of the panes is ‘FileOut’. If you select this you will create a file out of the item you selected. You can take the file, modify it and do a FileIn from the File List application. Just go to the ‘World’ popup menu, click on ‘open’ and then ‘file list’. Select the file you have just modified, click on the ‘file in’ button at the top of the application.

If you look carefully at the file that you generated from FileOut you’ll see the method categories are bounded with ‘!’. You need method categories. If you create a method without a method category you can’t do a FileIn anymore.

Play around a bit more with the System Browser until you’re comfortable. You’ll be using it a lot in my next post on creating a Seaside application.

Seaside, Squeak and Smalltalk

I had an interesting chat with Nick last week and he mentioned the concept of continuations to me. One thing led to another and soon I was mucking around with an web application framework called Seaside. Seaside is quite a different type of web application framework from what I’m used to (J2EE-types, Struts, Webwork or even Ruby on Rails). For one it’s a continuations-based web application framework. Also, it’s a web application framework built with Smalltalk. Yes, Smalltalk.

That’s a language I’ve not heard for a long time. I remembered learning about Smalltalk last during my university days and not much about it since. Smalltalk, for those uninitiated, is the granddaddy of all object-oriented languages, first introduced about 35 years ago in 1971 but really made it as a programming language as Smalltalk-80 (in 1980 of course). It’s the first language that has the ‘everything is an object’ paradigm. Seaside runs on a couple of variants of Smalltalk but the one that I used is Squeak. Squeak, derived from Smalltalk-80 is an open source Smalltalk variant developed originally by Apple programmers in 1996 and currently has an active development community.

But what’s so special about Seaside besides being a web application framework done in a niche programming language? Without going into the details (that’ll take a lot of writing), Seaside provides some interesting features that I’ve not seen in any other web application framework I know. For example, Seaside encourages developers to create web applications with a macro view of the application flow instead of individual page flows. Web applications are made from highly re-usable components (really Smalltalk objects) which are not tied to the flow of the application, can have its own control flow, its own state and contain can other components. Seaside allows developers to model application flows in a single piece of code compared to modelling it in disparate HTTP request handlers (think servlets in J2EE or action classes in Struts or controller methods in Rails). Frameworks like Struts or Webwork tries to model this kind of flow in static XML configuration files, which is at best, a hack. Rails tries to do this in its own code by convention, which is a good attempt but the way Seaside does it is the best I’ve seen so far.

Seaside is full of interesting bits and pieces done by various members of the Seaside community, which I’m just barely touching the tip of the iceberg. Ajax is supported through the inclusion of Scriptaculous, the incredibly useful library by Thomas Fuchs. Connectivity to relational databases like MySQL and PosgresSQL is available, though connecting to object-oriented databases like GOODS is more natural. There are also OR mapping libraries, and all the normal stuff you’ll expect from a web application framework.

However Seaside is not all a bed of roses. Firstly if you’re like me, coming from a mainly C++ and Java background, you’ll find Smalltalk a challenge to even read (though you can try ‘I Can Read C++ and Java But I Can’t Read Smalltalk‘). The Squeak environment is Windowed and GUI, which is familiar but not so familiar. The environment is based on the concept of ‘images’ that are snapshots of the entire environment. Development is through a built-in Systems Browser and incredibly also at the web browser (I kid you not) which fascinated and freaked me out a bit initially. But like most new tools, once you get used to it, it becomes more natural and comfortable.

I’ll be doing more write-ups on Squeak and Seaside in the next few blog entries. Stay posted!

Some good links to learn about Seaside besides the official web site.