Skip to main content

SMTP

The Test.Sandwich.Contexts.FakeSmtpServer module provides a simple fake SMTP server, useful for testing email systems. The server is based on a slightly tweaked version of ReachFive/fake-smtp-server.

This module is currently included in sandwich-contexts.

Example

Here's an example of introducing and using the server. The sendSampleEmail function here uses Network.HaskellNet.SMTP to send an email. Then, it waits for the email to appear by polling fakeSmtpServerGetEmails. We could test various properties of the email here if necessary.

https://github.com/codedownio/sandwich/blob/master/demos/demo-fake-smtp-server/app/Main.hs
spec :: TopSpec
spec = describe "Introducing a fake SMTP server" $
introduceNixContext nixpkgsReleaseDefault $ introduceFakeSmtpServerNix defaultFakeSmtpServerOptions $ do
it "sends an email and verifies it was received" $ do
FakeSmtpServer {..} <- getContext fakeSmtpServer
info [i|Got fake SMTP server on port: #{fakeSmtpServerSmtpPort}|]

sendSampleEmail fakeSmtpServerHostname fakeSmtpServerSmtpPort
waitUntil 60 $ do
fakeSmtpServerGetEmails >>= \case
[x] -> debug [i|Got email: #{x}|]
xs -> expectationFailure [i|Unexpected emails result: #{xs}|]