Sending mails in Common Lisp

Just a quickie that might be useful for people new to CL.

Install CL-SMTP if you don’t have it yet:

(asdf-install:install 'cl-smtp)

You’re now able to load it into your Lisp core at any time:

(asdf:oos 'asdf:load-op 'cl-smtp)

And use it, either interactively (HOST is your SMTP server, e.g. localhost):

(cl-smtp:send-email "HOST" "from@foo.com" "recipient@bar.com"
                    "SUBJECT" "BODY")

or non-interactively:

(handler-case
  (prog1 t
    (cl-smtp:send-email "HOST" "from@foo.com" "recipient@bar.com"
                        "SUBJECT" "BODY"))
  (error (msg) (format t "Could not send mail: ~A~%" msg) nil))

We need the prog1 because send-email always returns nil, even on success.
With the above, the whole expression will return true on success, which lets us decide afterwards whether we were able to hand our message to the mail server.

Adding accessiblity warnings to HTML-generating Lisp code

Image objects in HTML should have at least an ALT attribute for text browsers and screen readers, so let’s automate the checking:

(asdf:oos 'asdf:load-op 'cl-who)
 
(defmacro demand-image-attrs (img want-attrs have-attrs)
  "Check an image's accessiblity information for completeness."
  (let ((missing-attrs (gensym)))
    `(let* ((,missing-attrs (loop for attr in ,want-attrs
                                 when (not (find attr ,have-attrs))
                                 collect attr)))
       (unless (null ,missing-attrs)
         (warn "image ~S is missing the following attributes: ~{~A~^, ~}" ,img ,missing-attrs))))
 
(defmacro render-image (src &rest attrs)
  "Produce HTML code from an image location."
  (declare (type string src))
  `(demand-image-attrs src (list :alt :title) attrs)
  `(cl-who:with-html-output (*standard-output*)
     (:img :src ,src ,@attrs)))
 
CL-USER> (render-image "cat.png" :title "Tom")
WARNING: image "cat.png" is missing the following attributes: ALT
"<img src='cat.png' title='Tom' />"

Simple and effective.

CSS doesn’t scale

While customizing a CMS for a client, I was forced to confront an old enemy of mine: CSS.
“What”, you say, “but surely you must be convinced that CSS is the latest and greatest in the separation of style from content?”

Well, latest, yes. Greatest? Far from it.

The good points: CSS is the only standardized tool that lets you separate content. The cascading model is well-designed and most often well-implemented. The set of selectors and attributes is as well.

But now let’s face two glaring problems of CSS. And no, cross-browser compatibility hacks are not among them.

  1. dynamic layout: show me a CSS multi-column layout without at least one column fixed in width. With the scourge called
    I can specify percentages and they work nicely, adapting themselves to the screen estate of the client. Why this omission? It feels like stone age in a world full of handheld devices and lots of different resolutions.Most web designers seem fine with specifying pixels all around. But then again a lot of web designers have Macromedia Dreamweaver, Adobe Illustrator and Adobe Photoshop among their favourite tools. I’m always tempted to use tables instead, which gives me the additional boon of a working vertical alignment.
  2. arithmetic expressions: it’s not possible to do even the most simple of calculations like 2em + 50px. Also, wouldn’t it be badly needed to be able to say: 2em + (SCREEN-WIDTH / 2), especially with absolute positioning? In this case, I can actually see why CSS don’t has this, though: Microsoft Internet Explorer often messes up even with only one value.

What makes it really bad is that CSS3 doesn’t attempt to solve those, to my knowledge. But let’s see whether HTML5 with its semantic additions can help here.

What are your experiences with CSS?

Web applications for the elite

Weblocks is a very impressive web framework for Common Lisp, and my experiences of hacking away with it a few days have produced quite encouraging results.

Imagine a world where you don’t have to keep track of or prevent the user from

  • hitting reload
  • opening multiple pages in his browser
  • using his back and forward buttons

Sounds good? You’d like to be there? Weblocks offers all this — and more, like automatic usage of AJAX requests if supported on the client side.

The only catch:
You need a firm grip on functional programming and a basic understanding of Lisp to use it, but learning Lisp pays off in any case, and functional programming does incredibly more so.
It helps to have experimented with Hunchentoot before.
So, from LAMP to Weblocks is probably a no-go.

Today I found another framework which has not crossed my way before that looks promising: It’s called and is apparently built on Scheme, so it makes use of continuations as well.

Serving multi-lingual web content with Apache httpd

There is quite a bit of information on serving multiple versions of pages via Apache httpd’s content negotiation module, but Daniel Lorch’s notes on this topic is the best one I have seen so far.

It adresses all important issues and does it both straight to the point and in depth.

« Previous Page