Skip to content

Commit 09fcc49

Browse files
committed
add a webapi and webhooks for a simple http/json-based api
for applications to compose/send messages, receive delivery feedback, and maintain suppression lists. this is an alternative to applications using a library to compose messages, submitting those messages using smtp, and monitoring a mailbox with imap for DSNs, which can be processed into the equivalent of suppression lists. but you need to know about all these standards/protocols and find libraries. by using the webapi & webhooks, you just need a http & json library. unfortunately, there is no standard for these kinds of api, so mox has made up yet another one... matching incoming DSNs about deliveries to original outgoing messages requires keeping history of "retired" messages (delivered from the queue, either successfully or failed). this can be enabled per account. history is also useful for debugging deliveries. we now also keep history of each delivery attempt, accessible while still in the queue, and kept when a message is retired. the queue webadmin pages now also have pagination, to show potentially large history. a queue of webhook calls is now managed too. failures are retried similar to message deliveries. webhooks can also be saved to the retired list after completing. also configurable per account. messages can be sent with a "unique smtp mail from" address. this can only be used if the domain is configured with a localpart catchall separator such as "+". when enabled, a queued message gets assigned a random "fromid", which is added after the separator when sending. when DSNs are returned, they can be related to previously sent messages based on this fromid. in the future, we can implement matching on the "envid" used in the smtp dsn extension, or on the "message-id" of the message. using a fromid can be triggered by authenticating with a login email address that is configured as enabling fromid. suppression lists are automatically managed per account. if a delivery attempt results in certain smtp errors, the destination address is added to the suppression list. future messages queued for that recipient will immediately fail without a delivery attempt. suppression lists protect your mail server reputation. submitted messages can carry "extra" data through the queue and webhooks for outgoing deliveries. through webapi as a json object, through smtp submission as message headers of the form "x-mox-extra-<key>: value". to make it easy to test webapi/webhooks locally, the "localserve" mode actually puts messages in the queue. when it's time to deliver, it still won't do a full delivery attempt, but just delivers to the sender account. unless the recipient address has a special form, simulating a failure to deliver. admins now have more control over the queue. "hold rules" can be added to mark newly queued messages as "on hold", pausing delivery. rules can be about certain sender or recipient domains/addresses, or apply to all messages pausing the entire queue. also useful for (local) testing. new config options have been introduced. they are editable through the admin and/or account web interfaces. the webapi http endpoints are enabled for newly generated configs with the quickstart, and in localserve. existing configurations must explicitly enable the webapi in mox.conf. gopherwatch.org was created to dogfood this code. it initially used just the compose/smtpclient/imapclient mox packages to send messages and process delivery feedback. it will get a config option to use the mox webapi/webhooks instead. the gopherwatch code to use webapi/webhook is smaller and simpler, and developing that shaped development of the mox webapi/webhooks. for issue #31 by cuu508
1 parent 8bec5ef commit 09fcc49

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+15521
-1271
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ See Quickstart below to get started.
3333
support is limited).
3434
- Webserver with serving static files and forwarding requests (reverse
3535
proxy), so port 443 can also be used to serve websites.
36+
- Simple HTTP/JSON API for sending transaction email and receiving delivery
37+
events and incoming messages (webapi and webhooks).
3638
- Prometheus metrics and structured logging for operational insight.
3739
- "mox localserve" subcommand for running mox locally for email-related
3840
testing/developing, including pedantic mode.
@@ -133,12 +135,13 @@ https://nlnet.nl/project/Mox/.
133135

134136
## Roadmap
135137

138+
- Aliases, for delivering to multiple local accounts.
136139
- Webmail improvements
137-
- HTTP-based API for sending messages and receiving delivery feedback
138140
- Calendaring with CalDAV/iCal
139141
- More IMAP extensions (PREVIEW, WITHIN, IMPORTANT, COMPRESS=DEFLATE,
140142
CREATE-SPECIAL-USE, SAVEDATE, UNAUTHENTICATE, REPLACE, QUOTA, NOTIFY,
141143
MULTIAPPEND, OBJECTID, MULTISEARCH, THREAD, SORT)
144+
- SMTP DSN extension
142145
- ARC, with forwarded email from trusted source
143146
- Forwarding (to an external address)
144147
- Add special IMAP mailbox ("Queue?") that contains queued but
@@ -447,6 +450,23 @@ messages, for example by replacing your Message-Id header and thereby
447450
invalidating your DKIM-signatures, or rejecting messages with more than one
448451
DKIM-signature.
449452

453+
## Can I use mox to send transactional email?
454+
455+
Yes. While you can use SMTP submission to send messages you've composed
456+
yourself, and monitor a mailbox for DSNs, a more convenient option is to use
457+
the mox HTTP/JSON-based webapi and webhooks.
458+
459+
The mox webapi can be used to send outgoing messages that mox composes. The web
460+
api can also be used to deal with messages stored in an account, like changing
461+
message flags, retrieving messages in parsed form or individual parts of
462+
multipart messages, or moving messages to another mailbox or deleting messages
463+
altogether.
464+
465+
Mox webhooks can be used to receive updates about incoming and outgoing
466+
deliveries. Mox can automatically manage per account suppression lists.
467+
468+
See https://www.xmox.nl/features/#hdr-webapi-and-webhooks for details.
469+
450470
## Can I use existing TLS certificates/keys?
451471

452472
Yes. The quickstart command creates a config that uses ACME with Let's Encrypt,

apidiff/next.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Below are the incompatible changes between v0.0.10 and next, per package.
1313
# iprev
1414

1515
# message
16+
- (*Composer).TextPart: changed from func(string) ([]byte, string, string) to func(string, string) ([]byte, string, string)
1617
- From: changed from func(*log/slog.Logger, bool, io.ReaderAt) (github.com/mjl-/mox/smtp.Address, *Envelope, net/textproto.MIMEHeader, error) to func(*log/slog.Logger, bool, io.ReaderAt, *Part) (github.com/mjl-/mox/smtp.Address, *Envelope, net/textproto.MIMEHeader, error)
1718
- NewComposer: changed from func(io.Writer, int64) *Composer to func(io.Writer, int64, bool) *Composer
1819

apidiff/packages.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ spf
1616
subjectpass
1717
tlsrpt
1818
updates
19+
webapi
20+
webhook

config/config.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ type Listener struct {
182182
AdminHTTPS WebService `sconf:"optional" sconf-doc:"Admin web interface listener like AdminHTTP, but for HTTPS. Requires a TLS config."`
183183
WebmailHTTP WebService `sconf:"optional" sconf-doc:"Webmail client, for reading email. Default path is /webmail/."`
184184
WebmailHTTPS WebService `sconf:"optional" sconf-doc:"Webmail client, like WebmailHTTP, but for HTTPS. Requires a TLS config."`
185+
WebAPIHTTP WebService `sconf:"optional" sconf-doc:"Like WebAPIHTTP, but with plain HTTP, without TLS."`
186+
WebAPIHTTPS WebService `sconf:"optional" sconf-doc:"WebAPI, a simple HTTP/JSON-based API for email, with HTTPS (requires a TLS config). Default path is /webapi/."`
185187
MetricsHTTP struct {
186188
Enabled bool
187189
Port int `sconf:"optional" sconf-doc:"Default 8010."`
@@ -210,7 +212,7 @@ type Listener struct {
210212
} `sconf:"optional" sconf-doc:"All configured WebHandlers will serve on an enabled listener. Either ACME must be configured, or for each WebHandler domain a TLS certificate must be configured."`
211213
}
212214

213-
// WebService is an internal web interface: webmail, account, admin.
215+
// WebService is an internal web interface: webmail, webaccount, webadmin, webapi.
214216
type WebService struct {
215217
Enabled bool
216218
Port int `sconf:"optional" sconf-doc:"Default 80 for HTTP and 443 for HTTPS."`
@@ -356,6 +358,19 @@ type Route struct {
356358

357359
// todo: move RejectsMailbox to store.Mailbox.SpecialUse, possibly with "X" prefix?
358360

361+
// note: outgoing hook events are in ../queue/hooks.go, ../mox-/config.go, ../queue.go and ../webapi/gendoc.sh. keep in sync.
362+
363+
type OutgoingWebhook struct {
364+
URL string `sconf-doc:"URL to POST webhooks."`
365+
Authorization string `sconf:"optional" sconf-doc:"If not empty, value of Authorization header to add to HTTP requests."`
366+
Events []string `sconf:"optional" sconf-doc:"Events to send outgoing delivery notifications for. If absent, all events are sent. Valid values: delivered, suppressed, delayed, failed, relayed, expanded, canceled, unrecognized."`
367+
}
368+
369+
type IncomingWebhook struct {
370+
URL string `sconf-doc:"URL to POST webhooks to for incoming deliveries over SMTP."`
371+
Authorization string `sconf:"optional" sconf-doc:"If not empty, value of Authorization header to add to HTTP requests."`
372+
}
373+
359374
type SubjectPass struct {
360375
Period time.Duration `sconf-doc:"How long unique values are accepted after generating, e.g. 12h."` // todo: have a reasonable default for this?
361376
}
@@ -368,6 +383,12 @@ type AutomaticJunkFlags struct {
368383
}
369384

370385
type Account struct {
386+
OutgoingWebhook *OutgoingWebhook `sconf:"optional" sconf-doc:"Webhooks for events about outgoing deliveries."`
387+
IncomingWebhook *IncomingWebhook `sconf:"optional" sconf-doc:"Webhooks for events about incoming deliveries over SMTP."`
388+
FromIDLoginAddresses []string `sconf:"optional" sconf-doc:"Login addresses that cause outgoing email to be sent with SMTP MAIL FROM addresses with a unique id after the localpart catchall separator (which must be enabled when addresses are specified here). Any delivery status notifications (DSN, e.g. for bounces), can be related to the original message and recipient with unique id's. You can login to an account with any valid email address, including variants with the localpart catchall separator. You can use this mechanism to both send outgoing messages both with and without unique fromid for a given address."`
389+
KeepRetiredMessagePeriod time.Duration `sconf:"optional" sconf-doc:"Period to keep messages retired from the queue (delivered or failed) around. Keeping retired messages is useful for maintaining the suppression list for transactional email, for matching incoming DSNs to sent messages, and for debugging. The time at which to clean up (remove) is calculated at retire time. E.g. 168h (1 week)."`
390+
KeepRetiredWebhookPeriod time.Duration `sconf:"optional" sconf-doc:"Period to keep webhooks retired from the queue (delivered or failed) around. Useful for debugging. The time at which to clean up (remove) is calculated at retire time. E.g. 168h (1 week)."`
391+
371392
Domain string `sconf-doc:"Default domain for account. Deprecated behaviour: If a destination is not a full address but only a localpart, this domain is added to form a full address."`
372393
Description string `sconf:"optional" sconf-doc:"Free form description, e.g. full name or alternative contact info."`
373394
FullName string `sconf:"optional" sconf-doc:"Full name, to use in message From header when composing messages in webmail. Can be overridden per destination."`
@@ -383,10 +404,11 @@ type Account struct {
383404
NoFirstTimeSenderDelay bool `sconf:"optional" sconf-doc:"Do not apply a delay to SMTP connections before accepting an incoming message from a first-time sender. Can be useful for accounts that sends automated responses and want instant replies."`
384405
Routes []Route `sconf:"optional" sconf-doc:"Routes for delivering outgoing messages through the queue. Each delivery attempt evaluates these account routes, domain routes and finally global routes. The transport of the first matching route is used in the delivery attempt. If no routes match, which is the default with no configured routes, messages are delivered directly from the queue."`
385406

386-
DNSDomain dns.Domain `sconf:"-"` // Parsed form of Domain.
387-
JunkMailbox *regexp.Regexp `sconf:"-" json:"-"`
388-
NeutralMailbox *regexp.Regexp `sconf:"-" json:"-"`
389-
NotJunkMailbox *regexp.Regexp `sconf:"-" json:"-"`
407+
DNSDomain dns.Domain `sconf:"-"` // Parsed form of Domain.
408+
JunkMailbox *regexp.Regexp `sconf:"-" json:"-"`
409+
NeutralMailbox *regexp.Regexp `sconf:"-" json:"-"`
410+
NotJunkMailbox *regexp.Regexp `sconf:"-" json:"-"`
411+
ParsedFromIDLoginAddresses []smtp.Address `sconf:"-" json:"-"`
390412
}
391413

392414
type JunkFilter struct {

config/doc.go

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,35 @@ See https://pkg.go.dev/github.com/mjl-/sconf for details.
386386
# limiting and for the "secure" status of cookies. (optional)
387387
Forwarded: false
388388
389+
# Like WebAPIHTTP, but with plain HTTP, without TLS. (optional)
390+
WebAPIHTTP:
391+
Enabled: false
392+
393+
# Default 80 for HTTP and 443 for HTTPS. (optional)
394+
Port: 0
395+
396+
# Path to serve requests on. (optional)
397+
Path:
398+
399+
# If set, X-Forwarded-* headers are used for the remote IP address for rate
400+
# limiting and for the "secure" status of cookies. (optional)
401+
Forwarded: false
402+
403+
# WebAPI, a simple HTTP/JSON-based API for email, with HTTPS (requires a TLS
404+
# config). Default path is /webapi/. (optional)
405+
WebAPIHTTPS:
406+
Enabled: false
407+
408+
# Default 80 for HTTP and 443 for HTTPS. (optional)
409+
Port: 0
410+
411+
# Path to serve requests on. (optional)
412+
Path:
413+
414+
# If set, X-Forwarded-* headers are used for the remote IP address for rate
415+
# limiting and for the "secure" status of cookies. (optional)
416+
Forwarded: false
417+
389418
# Serve prometheus metrics, for monitoring. You should not enable this on a public
390419
# IP. (optional)
391420
MetricsHTTP:
@@ -855,6 +884,53 @@ See https://pkg.go.dev/github.com/mjl-/sconf for details.
855884
Accounts:
856885
x:
857886
887+
# Webhooks for events about outgoing deliveries. (optional)
888+
OutgoingWebhook:
889+
890+
# URL to POST webhooks.
891+
URL:
892+
893+
# If not empty, value of Authorization header to add to HTTP requests. (optional)
894+
Authorization:
895+
896+
# Events to send outgoing delivery notifications for. If absent, all events are
897+
# sent. Valid values: delivered, suppressed, delayed, failed, relayed, expanded,
898+
# canceled, unrecognized. (optional)
899+
Events:
900+
-
901+
902+
# Webhooks for events about incoming deliveries over SMTP. (optional)
903+
IncomingWebhook:
904+
905+
# URL to POST webhooks to for incoming deliveries over SMTP.
906+
URL:
907+
908+
# If not empty, value of Authorization header to add to HTTP requests. (optional)
909+
Authorization:
910+
911+
# Login addresses that cause outgoing email to be sent with SMTP MAIL FROM
912+
# addresses with a unique id after the localpart catchall separator (which must be
913+
# enabled when addresses are specified here). Any delivery status notifications
914+
# (DSN, e.g. for bounces), can be related to the original message and recipient
915+
# with unique id's. You can login to an account with any valid email address,
916+
# including variants with the localpart catchall separator. You can use this
917+
# mechanism to both send outgoing messages both with and without unique fromid for
918+
# a given address. (optional)
919+
FromIDLoginAddresses:
920+
-
921+
922+
# Period to keep messages retired from the queue (delivered or failed) around.
923+
# Keeping retired messages is useful for maintaining the suppression list for
924+
# transactional email, for matching incoming DSNs to sent messages, and for
925+
# debugging. The time at which to clean up (remove) is calculated at retire time.
926+
# E.g. 168h (1 week). (optional)
927+
KeepRetiredMessagePeriod: 0s
928+
929+
# Period to keep webhooks retired from the queue (delivered or failed) around.
930+
# Useful for debugging. The time at which to clean up (remove) is calculated at
931+
# retire time. E.g. 168h (1 week). (optional)
932+
KeepRetiredWebhookPeriod: 0s
933+
858934
# Default domain for account. Deprecated behaviour: If a destination is not a full
859935
# address but only a localpart, this domain is added to form a full address.
860936
Domain:
@@ -1233,8 +1309,8 @@ See https://pkg.go.dev/github.com/mjl-/sconf for details.
12331309
# Examples
12341310
12351311
Mox includes configuration files to illustrate common setups. You can see these
1236-
examples with "mox example", and print a specific example with "mox example
1237-
<name>". Below are all examples included in mox.
1312+
examples with "mox config example", and print a specific example with "mox
1313+
config example <name>". Below are all examples included in mox.
12381314
12391315
# Example webhandlers
12401316

0 commit comments

Comments
 (0)