mirror of
https://github.com/mjl-/mox.git
synced 2025-07-12 10:24:37 +03:00
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
This commit is contained in:
@ -1952,7 +1952,12 @@ func (Admin) SetPassword(ctx context.Context, accountName, password string) {
|
||||
|
||||
// AccountSettingsSave set new settings for an account that only an admin can set.
|
||||
func (Admin) AccountSettingsSave(ctx context.Context, accountName string, maxOutgoingMessagesPerDay, maxFirstTimeRecipientsPerDay int, maxMsgSize int64, firstTimeSenderDelay bool) {
|
||||
err := mox.AccountAdminSettingsSave(ctx, accountName, maxOutgoingMessagesPerDay, maxFirstTimeRecipientsPerDay, maxMsgSize, firstTimeSenderDelay)
|
||||
err := mox.AccountSave(ctx, accountName, func(acc *config.Account) {
|
||||
acc.MaxOutgoingMessagesPerDay = maxOutgoingMessagesPerDay
|
||||
acc.MaxFirstTimeRecipientsPerDay = maxFirstTimeRecipientsPerDay
|
||||
acc.QuotaMessageSize = maxMsgSize
|
||||
acc.NoFirstTimeSenderDelay = !firstTimeSenderDelay
|
||||
})
|
||||
xcheckf(ctx, err, "saving account settings")
|
||||
}
|
||||
|
||||
@ -2005,8 +2010,8 @@ func (Admin) QueueHoldRuleRemove(ctx context.Context, holdRuleID int64) {
|
||||
}
|
||||
|
||||
// QueueList returns the messages currently in the outgoing queue.
|
||||
func (Admin) QueueList(ctx context.Context, filter queue.Filter) []queue.Msg {
|
||||
l, err := queue.List(ctx, filter)
|
||||
func (Admin) QueueList(ctx context.Context, filter queue.Filter, sort queue.Sort) []queue.Msg {
|
||||
l, err := queue.List(ctx, filter, sort)
|
||||
xcheckf(ctx, err, "listing messages in queue")
|
||||
return l
|
||||
}
|
||||
@ -2066,6 +2071,59 @@ func (Admin) QueueTransportSet(ctx context.Context, filter queue.Filter, transpo
|
||||
return n
|
||||
}
|
||||
|
||||
// RetiredList returns messages retired from the queue (delivery could
|
||||
// have succeeded or failed).
|
||||
func (Admin) RetiredList(ctx context.Context, filter queue.RetiredFilter, sort queue.RetiredSort) []queue.MsgRetired {
|
||||
l, err := queue.RetiredList(ctx, filter, sort)
|
||||
xcheckf(ctx, err, "listing retired messages")
|
||||
return l
|
||||
}
|
||||
|
||||
// HookQueueSize returns the number of webhooks still to be delivered.
|
||||
func (Admin) HookQueueSize(ctx context.Context) int {
|
||||
n, err := queue.HookQueueSize(ctx)
|
||||
xcheckf(ctx, err, "get hook queue size")
|
||||
return n
|
||||
}
|
||||
|
||||
// HookList lists webhooks still to be delivered.
|
||||
func (Admin) HookList(ctx context.Context, filter queue.HookFilter, sort queue.HookSort) []queue.Hook {
|
||||
l, err := queue.HookList(ctx, filter, sort)
|
||||
xcheckf(ctx, err, "listing hook queue")
|
||||
return l
|
||||
}
|
||||
|
||||
// HookNextAttemptSet sets a new time for next delivery attempt of matching
|
||||
// hooks from the queue.
|
||||
func (Admin) HookNextAttemptSet(ctx context.Context, filter queue.HookFilter, minutes int) (affected int) {
|
||||
n, err := queue.HookNextAttemptSet(ctx, filter, time.Now().Add(time.Duration(minutes)*time.Minute))
|
||||
xcheckf(ctx, err, "setting new next delivery attempt time for matching webhooks in queue")
|
||||
return n
|
||||
}
|
||||
|
||||
// HookNextAttemptAdd adds a duration to the time of next delivery attempt of
|
||||
// matching hooks from the queue.
|
||||
func (Admin) HookNextAttemptAdd(ctx context.Context, filter queue.HookFilter, minutes int) (affected int) {
|
||||
n, err := queue.HookNextAttemptAdd(ctx, filter, time.Duration(minutes)*time.Minute)
|
||||
xcheckf(ctx, err, "adding duration to next delivery attempt for matching webhooks in queue")
|
||||
return n
|
||||
}
|
||||
|
||||
// HookRetiredList lists retired webhooks.
|
||||
func (Admin) HookRetiredList(ctx context.Context, filter queue.HookRetiredFilter, sort queue.HookRetiredSort) []queue.HookRetired {
|
||||
l, err := queue.HookRetiredList(ctx, filter, sort)
|
||||
xcheckf(ctx, err, "listing retired hooks")
|
||||
return l
|
||||
}
|
||||
|
||||
// HookCancel prevents further delivery attempts of matching webhooks.
|
||||
func (Admin) HookCancel(ctx context.Context, filter queue.HookFilter) (affected int) {
|
||||
log := pkglog.WithContext(ctx)
|
||||
n, err := queue.HookCancel(ctx, log, filter)
|
||||
xcheckf(ctx, err, "cancel hooks in queue")
|
||||
return n
|
||||
}
|
||||
|
||||
// LogLevels returns the current log levels.
|
||||
func (Admin) LogLevels(ctx context.Context) map[string]string {
|
||||
m := map[string]string{}
|
||||
|
Reference in New Issue
Block a user