technology from back to front

HTML email from Squeak using Seaside

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:

whateverObjectYouInstalledTheMethodOn
  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!']
by
tonyg
on
08/09/09
  1. You know about the cascades syntax, right?

  2. @Duncan: yes; why do you ask?

 
 
2000-9 LShift Ltd, 1st Floor Office, Hoxton Point, 6 Rufus Street, London, N1 6PE, UK +44 (0)20 7729 7060