Up: Part III

F Sending Email

Sending email is a common enough task (user registration, notifications, etc) within a web application that we’ve decided to cover it here. Although email isn’t Lift’s primary focus, Lift does provide some facilities to simplify email transmission.

F.1 Setup

Configuration of the mailer is handled in a few different ways. The net.liftweb.util.Mailer object defines a hostFunc function var, () ⇒ String, that is used to compute the hostname of your SMTP server to be used for transmission. The default value is a function that looks up the mail.smtp.host system property and uses that String. If that property isn’t defined then the mailer defaults to localhost. Setting the system property is the simplest way to change your SMTP relay, although you could also define your own function to return a custom hostname and assign it to Mailer.hostFunc.

F.2 Sending Emails

The mailer interface is simple but covers a wide variety of cases. The Mailer object defines a number of case classes that correspond to the components of an RFC822 email. The addressing and subject cases classes, From, To, CC, BCC, ReplyTo and Subject should all be self-explanatory. For the body of the email you have three main options:
PlainMailBodyType Represents a plain-text email body based on a given String
XHTMLMailBodyType Represents an XHTML email body based on a given NodeSeq
XHTMLPlusImages Similar to XHTMLMailBodyType, but in addition to the NodeSeq, you can provide one or more PlusImageHolder instances that represent images to be attached to the email (embedded images, so to speak)
The Mailer.sendMail function is used to generate and send an email. It takes three arguments: the From sender address, the Subject of the email, and a varargs list of recipient addresses and body components. The mailer creates MIME/Multipart messages, so you can send more than one body (i.e. plain text and XHMTL) if you would like. Listing F.2↓ shows an example of sending an email to a group of recipients in both plain text and XHTML format. The Mailer object defines some implicit conversions to PlainMailBodyType and XHTMLMailBodyType, which we use here. We also have to do a little List trickery to be able to squeeze multiple arguments into the final vararg argument since Scala doesn’t support mixing regular values and coerced sequences in vararg arguments.
Sending a two-part email
import net.liftweb.util.Mailer
import Mailer._
...
val myRecips : List[String] = ...
val plainContent : String = "..."
val xhtmlContent : NodeSeq = ...
​
Mailer.sendMail(From("no-reply@foo.com"), Subject("Just a test"),
                (plainContent :: xhtmlContent :: myRecips.map(To(_))) : _*)
When you call sendMail you’re actually sending a message to an actor in the background that will handle actual mail delivery; because of this, you shouldn’t expect to see a synchronous relay of the message through your SMTP server.
Up: Part III

(C) 2012 Lift 2.0 EditionWritten by Derek Chen-Becker, Marius Danciu and Tyler Weir