Chapter 9. Around Lift
This chapter looks at interacting with other systems from within Lift, such as sending email, calling URLs, or scheduling tasks.
Many of the recipes in this chapter have code examples in a project at https://github.com/LiftCookbook/cookbook_around.
Sending Plain-Text Email
Problem
You want to send a plain-text email from your Lift application.
Solution
Use the Mailer:
importnet.liftweb.util.Mailerimportnet.liftweb.util.Mailer._Mailer.sendMail(From("you@example.org"),Subject("Hello"),To("other@example.org"),PlainMailBodyType("Hello from Lift"))
Discussion
Mailer sends the message asynchronously, meaning sendMail will
return immediately, so you don’t have to worry about the time costs of
negotiating with an SMTP server. However, there’s also a blockingSendMail
method if you do need to wait.
By default, the SMTP server used will be localhost. You can change
this by setting the mail.smtp.host property.
For example, edit src/mail/resources/props/default.props and add the line:
.smtp.host=smtp.example.org
The signature of sendMail requires a From, Subject, and then any
number of MailTypes:
-
To,CC, andBCC - The recipient email address
-
ReplyTo - The address that mail clients should use for replies
-
MessageHeader - Key/value pairs to include as headers in the message
-
PlainMailBodyType - A plain-text email sent with UTF-8 encoding
-
PlainPlusBodyType - A plain-text email, where you specify the encoding
-
XHTMLMailBodyType - For HTML email (SMTP Authentication)
-
XHTMLPlusImages ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access