Letterbox.Routing (Letterbox v0.1.0-rc.0)

Copy Markdown View Source

Which mailbox takes which message: the app's routes, in one module.

defmodule MyApp.Mailboxes do
  use Letterbox.Routing

  route "support@example.com", to: MyApp.SupportMailbox
  route "billing@", to: MyApp.BillingMailbox
  route ~r/\Areply\+/, to: MyApp.RepliesMailbox
  route "@in.example.com", to: MyApp.SitesMailbox
  route &MyApp.Tickets.forwarded?/1, to: MyApp.ForwardsMailbox
  route :all, to: MyApp.UnknownMailbox
end

config :letterbox, routing: MyApp.Mailboxes

A route matches a message's recipients (Letterbox.Message's :recipients), each in its order, and the first route that matches one takes the message; the address it matched is the message's :recipient. A route is:

  • an address, "support@example.com": that address in any case, with or without a sub-address (support+urgent@example.com);
  • a local part, "billing@": at any domain, with or without a sub-address;
  • a domain, "@in.example.com": any address there;
  • a regex, matched with the address in lowercase;
  • a function of the message, true for the messages it takes (its :recipient is then the first of its recipients);
  • :all, every message.

A message no route takes is "unrouted".

Summary

Functions

The mailbox of a message and the recipient its route matched: {:ok, mailbox, recipient}, or :none.

A route: what it matches, and the mailbox (to:) that takes the messages.

Functions

match(routing, message)

@spec match(module(), Letterbox.Message.t()) ::
  {:ok, module(), Letterbox.Address.t() | nil} | :none

The mailbox of a message and the recipient its route matched: {:ok, mailbox, recipient}, or :none.

route(pattern, opts)

(macro)

A route: what it matches, and the mailbox (to:) that takes the messages.