HTML email from Squeak using Seaside

This page is a mirrored copy of an article originally posted on the LShift blog; see the archive index here.

Recently, as part of a Seaside-based application running within Squeak, I wanted to send HTML-formatted notification emails when certain things happened within the application.

It turns out that Squeak has a built-in SMTP client library, which with a small amount of glue can be used with Seaside’s HTML renderer to send HTML formatted emails using code similar to that used when rendering Seaside website components.

sendHtmlEmailTo: toEmailAddressString
  from: fromEmailAddressString
  subject: subjectString
  with: aBlock
    | m b bodyHtml |
    m := MailMessage empty.
    m setField: 'from' toString: fromEmailAddressString.
    m setField: 'to' toString: toEmailAddressString.
    m setField: 'subject' toString: subjectString.
    m setField: 'content-type' toString: 'text/html'.

    b := WAHtmlBuilder new.
    b canvasClass: WARenderCanvas.
    b rootClass: WAHtmlRoot.
    bodyHtml := b render: aBlock.

    m body: (MIMEDocument contentType: 'text/html' content: bodyHtml).
    SMTPClient deliverMailFrom: m from
               to: {m to}
               text: m asSendableText
               usingServer: 'YOUR.SMTP.SERVER.EXAMPLE.COM'.

The aBlock argument should be like the body of a WAComponent’s renderContentOn: method. Here’s an example:

  sendHtmlEmailTo: 'target@example.com'
  from: 'source@example.org'
  subject: 'Hello, world'
  with: [:html |
    html heading level3 with: 'This is a heading'.
    html paragraph with: 'Hi there!']


On 8 September, 2009 at 6:45 pm, Planet Ciaran (planetciaran) 's status on Tuesday, 08-Sep-09 19:44:20 BST - MicroCiaran wrote:

[...] tonyg - HTML email from Squeak using Seaside - http://www.lshift.net/blog/2009/09/08/html-email-from-squeak-using-seaside [...]

On 8 September, 2009 at 6:58 pm, Duncan wrote:

You know about the cascades syntax, right?

On 8 September, 2009 at 9:50 pm, tonyg wrote:

@Duncan: yes; why do you ask?