mirror of
https://github.com/hanzoai/mail.git
synced 2026-09-23 23:53:02 +00:00
Fork of github.com/mjl-/mox at 5656ca6d1b
(upstream v0.0.15 plus 55 commits).
Rename the module path in go.mod and the import paths in 268 Go files. Also
update the five scripts that "go install" the module (genwebsite.sh,
docker-release.sh, apidiff.sh, test-upgrade.sh, Dockerfile.release), which
would otherwise build upstream rather than this fork.
Add NOTICE recording the upstream project, author, license, and fork point,
and stating which license covers which files: MIT for the mox source and our
changes, MPL-2.0 for publicsuffix/public_suffix_list.txt alone, and the
vendored dependencies' own licenses under licenses/. Embed NOTICE so
"mail licenses" prints the lineage from the running binary. LICENSE.MIT,
LICENSE.MPLv2.0 and the licenses/ tree are preserved verbatim; no code is
relicensed.
The mox command name, config keys, data directory layout and website content
are unchanged. LLM.md records the fork point, the rename, what was left alone,
and the steps to stand the server up.
Co-authored-by: Hanzo Dev <[email protected]>
113 lines
3.2 KiB
Go
113 lines
3.2 KiB
Go
package queue
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"net"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/hanzoai/mail/dns"
|
|
"github.com/hanzoai/mail/dsn"
|
|
"github.com/hanzoai/mail/mlog"
|
|
"github.com/hanzoai/mail/mox-"
|
|
"github.com/hanzoai/mail/smtpclient"
|
|
"github.com/hanzoai/mail/store"
|
|
)
|
|
|
|
// We won't be dialing remote servers. We just connect the smtp port of the first
|
|
// ip in the "local" listener, with fallback to localhost:1025 for any destination
|
|
// address and try to deliver. Our smtpserver uses a mocked dns resolver to give
|
|
// spf/dkim a chance to pass.
|
|
func deliverLocalserve(ctx context.Context, log mlog.Log, msgs []*Msg, backoff time.Duration) {
|
|
m0 := msgs[0]
|
|
|
|
addr := "localhost:1025"
|
|
l, ok := mox.Conf.Static.Listeners["local"]
|
|
if ok && l.SMTP.Enabled {
|
|
port := 1025
|
|
if l.SMTP.Port != 0 {
|
|
port = l.SMTP.Port
|
|
}
|
|
addr = net.JoinHostPort(l.IPs[0], fmt.Sprintf("%d", port))
|
|
}
|
|
var d net.Dialer
|
|
dialctx, dialcancel := context.WithTimeout(ctx, 30*time.Second)
|
|
defer dialcancel()
|
|
conn, err := d.DialContext(dialctx, "tcp", addr)
|
|
dialcancel()
|
|
if err != nil {
|
|
failMsgsDB(log, msgs, m0.DialedIPs, backoff, dsn.NameIP{}, err)
|
|
return
|
|
}
|
|
defer func() {
|
|
if conn != nil {
|
|
err = conn.Close()
|
|
log.Check(err, "closing connection")
|
|
}
|
|
}()
|
|
|
|
clientctx, clientcancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer clientcancel()
|
|
localhost := dns.Domain{ASCII: "localhost"}
|
|
client, err := smtpclient.New(clientctx, log.Logger, conn, smtpclient.TLSOpportunistic, false, localhost, localhost, smtpclient.Opts{})
|
|
clientcancel()
|
|
if err != nil {
|
|
failMsgsDB(log, msgs, m0.DialedIPs, backoff, dsn.NameIP{}, err)
|
|
return
|
|
}
|
|
conn = nil // Will be closed when closing client.
|
|
defer func() {
|
|
err := client.Close()
|
|
log.Check(err, "closing smtp client")
|
|
}()
|
|
|
|
var msgr io.ReadCloser
|
|
var size int64
|
|
if len(m0.DSNUTF8) > 0 {
|
|
msgr = io.NopCloser(bytes.NewReader(m0.DSNUTF8))
|
|
size = int64(len(m0.DSNUTF8))
|
|
} else {
|
|
size = m0.Size
|
|
p := m0.MessagePath()
|
|
f, err := os.Open(p)
|
|
if err != nil {
|
|
log.Errorx("opening message for delivery", err, slog.String("remote", addr), slog.String("path", p))
|
|
err = fmt.Errorf("opening message file: %v", err)
|
|
failMsgsDB(log, msgs, m0.DialedIPs, backoff, dsn.NameIP{}, err)
|
|
return
|
|
}
|
|
msgr = store.FileMsgReader(m0.MsgPrefix, f)
|
|
defer func() {
|
|
if msgr != nil {
|
|
err := msgr.Close()
|
|
log.Check(err, "closing message after delivery attempt")
|
|
}
|
|
}()
|
|
}
|
|
|
|
deliverctx, delivercancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer delivercancel()
|
|
requireTLS := m0.RequireTLS != nil && *m0.RequireTLS
|
|
rcpts := make([]string, len(msgs))
|
|
for i, m := range msgs {
|
|
rcpts[i] = m.Recipient().String()
|
|
}
|
|
rcptErrs, err := client.DeliverMultiple(deliverctx, m0.Sender().String(), rcpts, size, msgr, m0.Has8bit, m0.SMTPUTF8, requireTLS)
|
|
delivercancel()
|
|
if err != nil {
|
|
log.Infox("smtp transaction for delivery failed", err)
|
|
}
|
|
|
|
// Must close before processing, because that may try to remove the message file,
|
|
// and on Windows we can't have it open when we remove it.
|
|
cerr := msgr.Close()
|
|
log.Check(cerr, "closing message after delivery attempt")
|
|
msgr = nil
|
|
|
|
processDeliveries(log, m0, msgs, addr, "localhost", backoff, rcptErrs, err)
|
|
}
|