Skip to content

Commit

Permalink
Shortened the example code, and noted that the default in testMode is…
Browse files Browse the repository at this point in the history
… to not send email.
  • Loading branch information
d6y authored and tjweir committed Mar 16, 2012
1 parent c7d5fd1 commit 9cd6d45
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions docs/0500-Around/0130-Logging-Email.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,39 @@ Logging email rather than sending

Problem
-------
You don't want email sent when developing your Lift application locally.
You don't want email sent when developing your Lift application locally, but you do want to see what would have been sent.


Solution
--------

The solution is to use `Mailer.devModeSend` and here is an example:

Declare an function and set a default hanlder in `Boot.scala`:
The solution is to use `Mailer.devModeSend` and here is an example for `Boot.scala`:

```scala
def email {
def toString(m: Any) = m match {
case mm: MimeMultipart => mm.getBodyPart(0).getContent
case otherwise => otherwise.toString
}

Mailer.devModeSend.default.set((m: MimeMessage) =>
log.info("Would have sent the following mail\n%s".format(toString(m.getContent()))))
}
import net.liftweb.util.Mailer._
import javax.mail.internet.{MimeMessage,MimeMultipart}

def stringify(m: Any) = m match {
case mm: MimeMultipart => mm.getBodyPart(0).getContent
case otherwise => otherwise.toString
}

Mailer.devModeSend.default.set( (m: MimeMessage) =>
logger.info("Would have sent "+stringify(m.getContent))
)
```

This example is changing the behaviour of `Mailer` when your Lift application is in developer mode (which it is by default). We are logging a message only, and using a utility function to get the contents of the first body part of the message. The key part is setting a `MimeMessage => Unit` function on `Mailer.devModeSend`.

Discussion
----------

When developing an application it is inconvient to have to worry about setting up an SMTP server or inadverntly sending test messages to users.
It may however be useful to know what would have been sent.
You can control how and if mail is sent using the `*ModeSend` functions available for the different Lift RunModes (dev,staging, production, profile and test).
When developing an application it is inconvenient to have to worry about setting up an SMTP server or inadvertently sending test messages to users.
The above is a useful way to know what would have been sent.

You can control how and if mail is sent using the `*ModeSend` functions available for the different Lift RunModes (dev, staging, production, profile, pilot and test). The default is to send email, except for `testModeSend`, which only logs the send.





0 comments on commit 9cd6d45

Please sign in to comment.