1// Package queue is in charge of outgoing messages, queueing them when submitted,
2// attempting a first delivery over SMTP, retrying with backoff and sending DSNs
3// for delayed or failed deliveries.
20 "golang.org/x/net/proxy"
22 "github.com/prometheus/client_golang/prometheus"
23 "github.com/prometheus/client_golang/prometheus/promauto"
25 "github.com/mjl-/bstore"
27 "github.com/mjl-/mox/config"
28 "github.com/mjl-/mox/dns"
29 "github.com/mjl-/mox/dsn"
30 "github.com/mjl-/mox/metrics"
31 "github.com/mjl-/mox/mlog"
32 "github.com/mjl-/mox/mox-"
33 "github.com/mjl-/mox/moxio"
34 "github.com/mjl-/mox/smtp"
35 "github.com/mjl-/mox/smtpclient"
36 "github.com/mjl-/mox/store"
37 "github.com/mjl-/mox/tlsrpt"
38 "github.com/mjl-/mox/tlsrptdb"
42 metricConnection = promauto.NewCounterVec(
43 prometheus.CounterOpts{
44 Name: "mox_queue_connection_total",
45 Help: "Queue client connections, outgoing.",
48 "result", // "ok", "timeout", "canceled", "error"
51 metricDelivery = promauto.NewHistogramVec(
52 prometheus.HistogramOpts{
53 Name: "mox_queue_delivery_duration_seconds",
54 Help: "SMTP client delivery attempt to single host.",
55 Buckets: []float64{0.01, 0.05, 0.100, 0.5, 1, 5, 10, 20, 30, 60, 120},
58 "attempt", // Number of attempts.
59 "transport", // empty for default direct delivery.
60 "tlsmode", // immediate, requiredstarttls, opportunistic, skip (from smtpclient.TLSMode), with optional +mtasts and/or +dane.
61 "result", // ok, timeout, canceled, temperror, permerror, error
66var jitter = mox.NewPseudoRand()
68var DBTypes = []any{Msg{}} // Types stored in DB.
69var DB *bstore.DB // Exported for making backups.
71// Allow requesting delivery starting from up to this interval from time of submission.
72const FutureReleaseIntervalMax = 60 * 24 * time.Hour
74// Set for mox localserve, to prevent queueing.
77// Msg is a message in the queue.
79// Use MakeMsg to make a message with fields that Add needs. Add will further set
80// queueing related fields.
84 // A message for multiple recipients will get a BaseID that is identical to the
85 // first Msg.ID queued. The message contents will be identical for each recipient,
86 // including MsgPrefix. If other properties are identical too, including recipient
87 // domain, multiple Msgs may be delivered in a single SMTP transaction. For
88 // messages with a single recipient, this field will be 0.
89 BaseID int64 `bstore:"index"`
91 Queued time.Time `bstore:"default now"`
92 SenderAccount string // Failures are delivered back to this local account. Also used for routing.
93 SenderLocalpart smtp.Localpart // Should be a local user and domain.
94 SenderDomain dns.IPDomain
95 RecipientLocalpart smtp.Localpart // Typically a remote user and domain.
96 RecipientDomain dns.IPDomain
97 RecipientDomainStr string // For filtering.
98 Attempts int // Next attempt is based on last attempt and exponential back off based on attempts.
99 MaxAttempts int // Max number of attempts before giving up. If 0, then the default of 8 attempts is used instead.
100 DialedIPs map[string][]net.IP // For each host, the IPs that were dialed. Used for IP selection for later attempts.
101 NextAttempt time.Time // For scheduling.
102 LastAttempt *time.Time
105 Has8bit bool // Whether message contains bytes with high bit set, determines whether 8BITMIME SMTP extension is needed.
106 SMTPUTF8 bool // Whether message requires use of SMTPUTF8.
107 IsDMARCReport bool // Delivery failures for DMARC reports are handled differently.
108 IsTLSReport bool // Delivery failures for TLS reports are handled differently.
109 Size int64 // Full size of message, combined MsgPrefix with contents of message file.
110 MessageID string // Used when composing a DSN, in its References header.
113 // If set, this message is a DSN and this is a version using utf-8, for the case
114 // the remote MTA supports smtputf8. In this case, Size and MsgPrefix are not
118 // If non-empty, the transport to use for this message. Can be set through cli or
119 // admin interface. If empty (the default for a submitted message), regular routing
123 // RequireTLS influences TLS verification during delivery.
125 // If nil, the recipient domain policy is followed (MTA-STS and/or DANE), falling
126 // back to optional opportunistic non-verified STARTTLS.
128 // If RequireTLS is true (through SMTP REQUIRETLS extension or webmail submit),
129 // MTA-STS or DANE is required, as well as REQUIRETLS support by the next hop
132 // If RequireTLS is false (through messag header "TLS-Required: No"), the recipient
133 // domain's policy is ignored if it does not lead to a successful TLS connection,
134 // i.e. falling back to SMTP delivery with unverified STARTTLS or plain text.
138 // For DSNs, where the original FUTURERELEASE value must be included as per-message
139 // field. This field should be of the form "for;" plus interval, or "until;" plus
141 FutureReleaseRequest string
145// Sender of message as used in MAIL FROM.
146func (m Msg) Sender() smtp.Path {
147 return smtp.Path{Localpart: m.SenderLocalpart, IPDomain: m.SenderDomain}
150// Recipient of message as used in RCPT TO.
151func (m Msg) Recipient() smtp.Path {
152 return smtp.Path{Localpart: m.RecipientLocalpart, IPDomain: m.RecipientDomain}
155// MessagePath returns the path where the message is stored.
156func (m Msg) MessagePath() string {
157 return mox.DataDirPath(filepath.Join("queue", store.MessagePath(m.ID)))
160// Init opens the queue database without starting delivery.
162 qpath := mox.DataDirPath(filepath.FromSlash("queue/index.db"))
163 os.MkdirAll(filepath.Dir(qpath), 0770)
165 if _, err := os.Stat(qpath); err != nil && os.IsNotExist(err) {
170 DB, err = bstore.Open(mox.Shutdown, qpath, &bstore.Options{Timeout: 5 * time.Second, Perm: 0660}, DBTypes...)
175 return fmt.Errorf("open queue database: %s", err)
180// Shutdown closes the queue database. The delivery process isn't stopped. For tests only.
184 mlog.New("queue", nil).Errorx("closing queue db", err)
189// List returns all messages in the delivery queue.
190// Ordered by earliest delivery attempt first.
191func List(ctx context.Context) ([]Msg, error) {
192 qmsgs, err := bstore.QueryDB[Msg](ctx, DB).List()
196 sort.Slice(qmsgs, func(i, j int) bool {
199 la := a.LastAttempt != nil
200 lb := b.LastAttempt != nil
203 } else if la && !lb {
206 if !la && !lb || a.LastAttempt.Equal(*b.LastAttempt) {
209 return a.LastAttempt.Before(*b.LastAttempt)
214// Count returns the number of messages in the delivery queue.
215func Count(ctx context.Context) (int, error) {
216 return bstore.QueryDB[Msg](ctx, DB).Count()
219// MakeMsg is a convenience function that sets the commonly used fields for a Msg.
220func MakeMsg(sender, recipient smtp.Path, has8bit, smtputf8 bool, size int64, messageID string, prefix []byte, requireTLS *bool, next time.Time) Msg {
222 SenderLocalpart: sender.Localpart,
223 SenderDomain: sender.IPDomain,
224 RecipientLocalpart: recipient.Localpart,
225 RecipientDomain: recipient.IPDomain,
226 RecipientDomainStr: formatIPDomain(recipient.IPDomain),
230 MessageID: messageID,
232 RequireTLS: requireTLS,
238// Add one or more new messages to the queue. They'll get the same BaseID, so they
239// can be delivered in a single SMTP transaction, with a single DATA command, but
240// may be split into multiple transactions if errors/limits are encountered. The
241// queue is kicked immediately to start a first delivery attempt.
243// ID of the messagse must be 0 and will be set after inserting in the queue.
245// Add sets derived fields like RecipientDomainStr, and fields related to queueing,
246// such as Queued, NextAttempt, LastAttempt, LastError.
247func Add(ctx context.Context, log mlog.Log, senderAccount string, msgFile *os.File, qml ...Msg) error {
249 return fmt.Errorf("must queue at least one message")
252 for _, qm := range qml {
254 return fmt.Errorf("id of queued messages must be 0")
256 if qm.RecipientDomainStr == "" {
257 return fmt.Errorf("recipient domain cannot be empty")
259 // Sanity check, internal consistency.
260 rcptDom := formatIPDomain(qm.RecipientDomain)
261 if qm.RecipientDomainStr != rcptDom {
262 return fmt.Errorf("mismatch between recipient domain and string form of domain")
267 if senderAccount == "" {
268 return fmt.Errorf("cannot queue with localserve without local account")
270 acc, err := store.OpenAccount(log, senderAccount)
272 return fmt.Errorf("opening sender account for immediate delivery with localserve: %v", err)
276 log.Check(err, "closing account")
278 conf, _ := acc.Conf()
280 acc.WithWLock(func() {
281 for i, qm := range qml {
282 qml[i].SenderAccount = senderAccount
283 m := store.Message{Size: qm.Size, MsgPrefix: qm.MsgPrefix}
284 dest := conf.Destinations[qm.Sender().String()]
285 err = acc.DeliverDestination(log, dest, &m, msgFile)
287 err = fmt.Errorf("delivering message: %v", err)
288 return // Returned again outside WithWLock.
293 log.Debug("immediately delivered from queue to sender")
298 tx, err := DB.Begin(ctx, true)
300 return fmt.Errorf("begin transaction: %w", err)
304 if err := tx.Rollback(); err != nil {
305 log.Errorx("rollback for queue", err)
310 // Insert messages into queue. If there are multiple messages, they all get a
311 // non-zero BaseID that is the Msg.ID of the first message inserted.
314 qml[i].SenderAccount = senderAccount
315 qml[i].BaseID = baseID
316 if err := tx.Insert(&qml[i]); err != nil {
319 if i == 0 && len(qml) > 1 {
321 qml[i].BaseID = baseID
322 if err := tx.Update(&qml[i]); err != nil {
330 for _, p := range paths {
332 log.Check(err, "removing destination message file for queue", slog.String("path", p))
336 for _, qm := range qml {
337 dst := qm.MessagePath()
338 paths = append(paths, dst)
339 dstDir := filepath.Dir(dst)
340 os.MkdirAll(dstDir, 0770)
341 if err := moxio.LinkOrCopy(log, dst, msgFile.Name(), nil, true); err != nil {
342 return fmt.Errorf("linking/copying message to new file: %s", err)
343 } else if err := moxio.SyncDir(log, dstDir); err != nil {
344 return fmt.Errorf("sync directory: %v", err)
348 if err := tx.Commit(); err != nil {
349 return fmt.Errorf("commit transaction: %s", err)
358func formatIPDomain(d dns.IPDomain) string {
360 return "[" + d.IP.String() + "]"
362 return d.Domain.Name()
366 kick = make(chan struct{}, 1)
367 deliveryResults = make(chan string, 1)
372 case kick <- struct{}{}:
377// Kick sets the NextAttempt for messages matching all filter parameters (ID,
378// toDomain, recipient) that are nonzero, and kicks the queue, attempting delivery
379// of those messages. If all parameters are zero, all messages are kicked. If
380// transport is set, the delivery attempts for the matching messages will use the
381// transport. An empty string is the default transport, i.e. direct delivery.
382// Returns number of messages queued for immediate delivery.
383func Kick(ctx context.Context, ID int64, toDomain, recipient string, transport *string) (int, error) {
384 q := bstore.QueryDB[Msg](ctx, DB)
389 q.FilterEqual("RecipientDomainStr", toDomain)
392 q.FilterFn(func(qm Msg) bool {
393 return qm.Recipient().XString(true) == recipient
396 up := map[string]any{"NextAttempt": time.Now()}
397 if transport != nil {
398 if *transport != "" {
399 _, ok := mox.Conf.Static.Transports[*transport]
401 return 0, fmt.Errorf("unknown transport %q", *transport)
404 up["Transport"] = *transport
406 n, err := q.UpdateFields(up)
408 return 0, fmt.Errorf("selecting and updating messages in queue: %v", err)
414// Drop removes messages from the queue that match all nonzero parameters.
415// If all parameters are zero, all messages are removed.
416// Returns number of messages removed.
417func Drop(ctx context.Context, log mlog.Log, ID int64, toDomain string, recipient string) (int, error) {
418 q := bstore.QueryDB[Msg](ctx, DB)
423 q.FilterEqual("RecipientDomainStr", toDomain)
426 q.FilterFn(func(qm Msg) bool {
427 return qm.Recipient().XString(true) == recipient
434 return 0, fmt.Errorf("selecting and deleting messages from queue: %v", err)
436 for _, m := range msgs {
438 if err := os.Remove(p); err != nil {
439 log.Errorx("removing queue message from file system", err, slog.Int64("queuemsgid", m.ID), slog.String("path", p))
445// SaveRequireTLS updates the RequireTLS field of the message with id.
446func SaveRequireTLS(ctx context.Context, id int64, requireTLS *bool) error {
447 return DB.Write(ctx, func(tx *bstore.Tx) error {
449 if err := tx.Get(&m); err != nil {
450 return fmt.Errorf("get message: %w", err)
452 m.RequireTLS = requireTLS
457type ReadReaderAtCloser interface {
462// OpenMessage opens a message present in the queue.
463func OpenMessage(ctx context.Context, id int64) (ReadReaderAtCloser, error) {
465 err := DB.Get(ctx, &qm)
469 f, err := os.Open(qm.MessagePath())
471 return nil, fmt.Errorf("open message file: %s", err)
473 r := store.FileMsgReader(qm.MsgPrefix, f)
477const maxConcurrentDeliveries = 10
479// Start opens the database by calling Init, then starts the delivery process.
480func Start(resolver dns.Resolver, done chan struct{}) error {
481 if err := Init(); err != nil {
485 log := mlog.New("queue", nil)
489 // Map keys are either dns.Domain.Name()'s, or string-formatted IP addresses.
490 busyDomains := map[string]struct{}{}
492 timer := time.NewTimer(0)
496 case <-mox.Shutdown.Done():
501 case domain := <-deliveryResults:
502 delete(busyDomains, domain)
505 if len(busyDomains) >= maxConcurrentDeliveries {
509 launchWork(log, resolver, busyDomains)
510 timer.Reset(nextWork(mox.Shutdown, log, busyDomains))
516func nextWork(ctx context.Context, log mlog.Log, busyDomains map[string]struct{}) time.Duration {
517 q := bstore.QueryDB[Msg](ctx, DB)
518 if len(busyDomains) > 0 {
520 for d := range busyDomains {
521 doms = append(doms, d)
523 q.FilterNotEqual("RecipientDomainStr", doms...)
525 q.SortAsc("NextAttempt")
528 if err == bstore.ErrAbsent {
529 return 24 * time.Hour
530 } else if err != nil {
531 log.Errorx("finding time for next delivery attempt", err)
532 return 1 * time.Minute
534 return time.Until(qm.NextAttempt)
537func launchWork(log mlog.Log, resolver dns.Resolver, busyDomains map[string]struct{}) int {
538 q := bstore.QueryDB[Msg](mox.Shutdown, DB)
539 q.FilterLessEqual("NextAttempt", time.Now())
540 q.SortAsc("NextAttempt")
541 q.Limit(maxConcurrentDeliveries)
542 if len(busyDomains) > 0 {
544 for d := range busyDomains {
545 doms = append(doms, d)
547 q.FilterNotEqual("RecipientDomainStr", doms...)
550 seen := map[string]bool{}
551 err := q.ForEach(func(m Msg) error {
552 dom := m.RecipientDomainStr
553 if _, ok := busyDomains[dom]; !ok && !seen[dom] {
555 msgs = append(msgs, m)
560 log.Errorx("querying for work in queue", err)
561 mox.Sleep(mox.Shutdown, 1*time.Second)
565 for _, m := range msgs {
566 busyDomains[m.RecipientDomainStr] = struct{}{}
567 go deliver(log, resolver, m)
572// Remove message from queue in database and file system.
573func queueDelete(ctx context.Context, msgIDs ...int64) error {
574 err := DB.Write(ctx, func(tx *bstore.Tx) error {
575 for _, id := range msgIDs {
576 if err := tx.Delete(&Msg{ID: id}); err != nil {
585 // If removing from database fails, we'll also leave the file in the file system.
588 for _, id := range msgIDs {
589 p := mox.DataDirPath(filepath.Join("queue", store.MessagePath(id)))
590 if err := os.Remove(p); err != nil {
591 errs = append(errs, fmt.Sprintf("%s: %v", p, err))
595 return fmt.Errorf("removing message files from queue: %s", strings.Join(errs, "; "))
600// deliver attempts to deliver a message.
601// The queue is updated, either by removing a delivered or permanently failed
602// message, or updating the time for the next attempt. A DSN may be sent.
603func deliver(log mlog.Log, resolver dns.Resolver, m Msg) {
606 qlog := log.WithCid(mox.Cid()).With(
607 slog.Any("from", m.Sender()),
608 slog.Int("attempts", m.Attempts))
611 deliveryResults <- formatIPDomain(m.RecipientDomain)
615 qlog.Error("deliver panic", slog.Any("panic", x), slog.Int64("msgid", m.ID), slog.Any("recipient", m.Recipient()))
617 metrics.PanicInc(metrics.Queue)
621 // We register this attempt by setting last_attempt, and already next_attempt time
622 // in the future with exponential backoff. If we run into trouble delivery below,
623 // at least we won't be bothering the receiving server with our problems.
624 // Delivery attempts: immediately, 7.5m, 15m, 30m, 1h, 2h (send delayed DSN), 4h,
625 // 8h, 16h (send permanent failure DSN).
628 backoff := time.Duration(7*60+30+jitter.Intn(10)-5) * time.Second
629 for i := 0; i < m.Attempts; i++ {
630 backoff *= time.Duration(2)
633 origNextAttempt := m.NextAttempt
636 m.NextAttempt = now.Add(backoff)
637 qup := bstore.QueryDB[Msg](mox.Shutdown, DB)
639 update := Msg{Attempts: m.Attempts, NextAttempt: m.NextAttempt, LastAttempt: m.LastAttempt}
640 if _, err := qup.UpdateNonzero(update); err != nil {
641 qlog.Errorx("storing delivery attempt", err, slog.Int64("msgid", m.ID), slog.Any("recipient", m.Recipient()))
645 resolveTransport := func(mm Msg) (string, config.Transport, bool) {
646 if mm.Transport != "" {
647 transport, ok := mox.Conf.Static.Transports[mm.Transport]
649 return "", config.Transport{}, false
651 return mm.Transport, transport, ok
653 route := findRoute(mm.Attempts, mm)
654 return route.Transport, route.ResolvedTransport, true
657 // Find route for transport to use for delivery attempt.
659 transportName, transport, transportOK := resolveTransport(m)
663 fail(ctx, qlog, []*Msg{&m}, m.DialedIPs, backoff, remoteMTA, fmt.Errorf("cannot find transport %q", m.Transport))
667 if transportName != "" {
668 qlog = qlog.With(slog.String("transport", transportName))
669 qlog.Debug("delivering with transport")
672 // Attempt to gather more recipients for this identical message, only with the same
673 // recipient domain, and under the same conditions (recipientdomain, attempts,
677 err := DB.Write(mox.Shutdown, func(tx *bstore.Tx) error {
678 q := bstore.QueryTx[Msg](tx)
679 q.FilterNonzero(Msg{BaseID: m.BaseID, RecipientDomainStr: m.RecipientDomainStr, Attempts: m.Attempts - 1})
680 q.FilterNotEqual("ID", m.ID)
681 q.FilterLessEqual("NextAttempt", origNextAttempt)
682 err := q.ForEach(func(xm Msg) error {
683 mrtls := m.RequireTLS != nil
684 xmrtls := xm.RequireTLS != nil
685 if mrtls != xmrtls || mrtls && *m.RequireTLS != *xm.RequireTLS {
688 tn, _, ok := resolveTransport(xm)
689 if ok && tn == transportName {
690 msgs = append(msgs, &xm)
695 return fmt.Errorf("looking up more recipients: %v", err)
698 // Mark these additional messages as attempted too.
699 for _, mm := range msgs[1:] {
701 mm.NextAttempt = m.NextAttempt
702 mm.LastAttempt = m.LastAttempt
703 if err := tx.Update(mm); err != nil {
704 return fmt.Errorf("updating more message recipients for smtp transaction: %v", err)
710 qlog.Errorx("error finding more recipients for message, will attempt to send to single recipient", err)
715 ids := make([]int64, len(msgs))
716 rcpts := make([]smtp.Path, len(msgs))
717 for i, m := range msgs {
719 rcpts[i] = m.Recipient()
721 qlog.Debug("delivering to multiple recipients", slog.Any("msgids", ids), slog.Any("recipients", rcpts))
723 qlog.Debug("delivering to single recipient", slog.Any("msgid", m.ID), slog.Any("recipient", m.Recipient()))
726 // We gather TLS connection successes and failures during delivery, and we store
727 // them in tlsrptdb. Every 24 hours we send an email with a report to the recipient
728 // domains that opt in via a TLSRPT DNS record. For us, the tricky part is
729 // collecting all reporting information. We've got several TLS modes
730 // (opportunistic, DANE and/or MTA-STS (PKIX), overrides due to Require TLS).
731 // Failures can happen at various levels: MTA-STS policies (apply to whole delivery
732 // attempt/domain), MX targets (possibly multiple per delivery attempt, both for
733 // MTA-STS and DANE).
735 // Once the SMTP client has tried a TLS handshake, we register success/failure,
736 // regardless of what happens next on the connection. We also register failures
737 // when they happen before we get to the SMTP client, but only if they are related
738 // to TLS (and some DNSSEC).
739 var recipientDomainResult tlsrpt.Result
740 var hostResults []tlsrpt.Result
742 if mox.Conf.Static.NoOutgoingTLSReports || m.RecipientDomain.IsIP() {
747 dayUTC := now.UTC().Format("20060102")
749 // See if this contains a failure. If not, we'll mark TLS results for delivering
750 // DMARC reports SendReport false, so we won't as easily get into a report sending
753 for _, result := range hostResults {
754 if result.Summary.TotalFailureSessionCount > 0 {
759 if recipientDomainResult.Summary.TotalFailureSessionCount > 0 {
763 results := make([]tlsrptdb.TLSResult, 0, 1+len(hostResults))
764 tlsaPolicyDomains := map[string]bool{}
765 addResult := func(r tlsrpt.Result, isHost bool) {
766 var zerotype tlsrpt.PolicyType
767 if r.Policy.Type == zerotype {
771 // Ensure we store policy domain in unicode in database.
772 policyDomain, err := dns.ParseDomain(r.Policy.Domain)
774 qlog.Errorx("parsing policy domain for tls result", err, slog.String("policydomain", r.Policy.Domain))
778 if r.Policy.Type == tlsrpt.TLSA {
779 tlsaPolicyDomains[policyDomain.ASCII] = true
782 tlsResult := tlsrptdb.TLSResult{
783 PolicyDomain: policyDomain.Name(),
785 RecipientDomain: m.RecipientDomain.Domain.Name(),
787 SendReport: !m.IsTLSReport && (!m.IsDMARCReport || failure),
788 Results: []tlsrpt.Result{r},
790 results = append(results, tlsResult)
792 for _, result := range hostResults {
793 addResult(result, true)
795 // If we were delivering to a mail host directly (not a domain with MX records), we
796 // are more likely to get a TLSA policy than an STS policy. Don't potentially
797 // confuse operators with both a tlsa and no-policy-found result.
799 if recipientDomainResult.Policy.Type != tlsrpt.NoPolicyFound || !tlsaPolicyDomains[recipientDomainResult.Policy.Domain] {
800 addResult(recipientDomainResult, false)
803 if len(results) > 0 {
804 err := tlsrptdb.AddTLSResults(context.Background(), results)
805 qlog.Check(err, "adding tls results to database for upcoming tlsrpt report")
809 var dialer smtpclient.Dialer = &net.Dialer{}
810 if transport.Submissions != nil {
811 deliverSubmit(qlog, resolver, dialer, msgs, backoff, transportName, transport.Submissions, true, 465)
812 } else if transport.Submission != nil {
813 deliverSubmit(qlog, resolver, dialer, msgs, backoff, transportName, transport.Submission, false, 587)
814 } else if transport.SMTP != nil {
815 // todo future: perhaps also gather tlsrpt results for submissions.
816 deliverSubmit(qlog, resolver, dialer, msgs, backoff, transportName, transport.SMTP, false, 25)
818 ourHostname := mox.Conf.Static.HostnameDomain
819 if transport.Socks != nil {
820 socksdialer, err := proxy.SOCKS5("tcp", transport.Socks.Address, nil, &net.Dialer{})
822 fail(ctx, qlog, msgs, msgs[0].DialedIPs, backoff, dsn.NameIP{}, fmt.Errorf("socks dialer: %v", err))
824 } else if d, ok := socksdialer.(smtpclient.Dialer); !ok {
825 fail(ctx, qlog, msgs, msgs[0].DialedIPs, backoff, dsn.NameIP{}, fmt.Errorf("socks dialer is not a contextdialer"))
830 ourHostname = transport.Socks.Hostname
832 recipientDomainResult, hostResults = deliverDirect(qlog, resolver, dialer, ourHostname, transportName, msgs, backoff)
836func findRoute(attempt int, m Msg) config.Route {
837 routesAccount, routesDomain, routesGlobal := mox.Conf.Routes(m.SenderAccount, m.SenderDomain.Domain)
838 if r, ok := findRouteInList(attempt, m, routesAccount); ok {
841 if r, ok := findRouteInList(attempt, m, routesDomain); ok {
844 if r, ok := findRouteInList(attempt, m, routesGlobal); ok {
847 return config.Route{}
850func findRouteInList(attempt int, m Msg, routes []config.Route) (config.Route, bool) {
851 for _, r := range routes {
852 if routeMatch(attempt, m, r) {
856 return config.Route{}, false
859func routeMatch(attempt int, m Msg, r config.Route) bool {
860 return attempt >= r.MinimumAttempts && routeMatchDomain(r.FromDomainASCII, m.SenderDomain.Domain) && routeMatchDomain(r.ToDomainASCII, m.RecipientDomain.Domain)
863func routeMatchDomain(l []string, d dns.Domain) bool {
867 for _, e := range l {
868 if d.ASCII == e || strings.HasPrefix(e, ".") && (d.ASCII == e[1:] || strings.HasSuffix(d.ASCII, e)) {
875// Returns string representing delivery result for err, and number of delivered and
878// Values: ok, okpartial, timeout, canceled, temperror, permerror, error.
879func deliveryResult(err error, delivered, failed int) string {
880 var cerr smtpclient.Error
885 } else if failed > 0 {
889 case errors.Is(err, os.ErrDeadlineExceeded), errors.Is(err, context.DeadlineExceeded):
891 case errors.Is(err, context.Canceled):
893 case errors.As(err, &cerr):