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]>
80 lines
2.8 KiB
Go
80 lines
2.8 KiB
Go
package smtpclient_test
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"log"
|
|
"log/slog"
|
|
"net"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/mail/dns"
|
|
"github.com/hanzoai/mail/sasl"
|
|
"github.com/hanzoai/mail/smtpclient"
|
|
)
|
|
|
|
func ExampleClient() {
|
|
// Submit a message to an SMTP server, with authentication. The SMTP server is
|
|
// responsible for getting the message delivered.
|
|
|
|
// Make TCP connection to submission server.
|
|
conn, err := net.Dial("tcp", "submit.example.org:465")
|
|
if err != nil {
|
|
log.Fatalf("dial submission server: %v", err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
// Initialize the SMTP session, with a EHLO, STARTTLS and authentication.
|
|
// Verify the server TLS certificate with PKIX/WebPKI.
|
|
ctx := context.Background()
|
|
tlsVerifyPKIX := true
|
|
opts := smtpclient.Opts{
|
|
Auth: func(mechanisms []string, cs *tls.ConnectionState) (sasl.Client, error) {
|
|
// If the server is known to support a SCRAM PLUS variant, you should only use
|
|
// that, detecting and preventing authentication mechanism downgrade attacks
|
|
// through TLS channel binding.
|
|
username := "mjl"
|
|
password := "test1234"
|
|
|
|
// Prefer strongest authentication mechanism, allow up to older CRAM-MD5.
|
|
if cs != nil && slices.Contains(mechanisms, "SCRAM-SHA-256-PLUS") {
|
|
return sasl.NewClientSCRAMSHA256PLUS(username, password, *cs), nil
|
|
}
|
|
if slices.Contains(mechanisms, "SCRAM-SHA-256") {
|
|
return sasl.NewClientSCRAMSHA256(username, password, true), nil
|
|
}
|
|
if cs != nil && slices.Contains(mechanisms, "SCRAM-SHA-1-PLUS") {
|
|
return sasl.NewClientSCRAMSHA1PLUS(username, password, *cs), nil
|
|
}
|
|
if slices.Contains(mechanisms, "SCRAM-SHA-1") {
|
|
return sasl.NewClientSCRAMSHA1(username, password, true), nil
|
|
}
|
|
if slices.Contains(mechanisms, "CRAM-MD5") {
|
|
return sasl.NewClientCRAMMD5(username, password), nil
|
|
}
|
|
// No mutually supported mechanism found, connection will fail.
|
|
return nil, nil
|
|
},
|
|
}
|
|
localname := dns.Domain{ASCII: "localhost"}
|
|
remotename := dns.Domain{ASCII: "submit.example.org"}
|
|
client, err := smtpclient.New(ctx, slog.Default(), conn, smtpclient.TLSImmediate, tlsVerifyPKIX, localname, remotename, opts)
|
|
if err != nil {
|
|
log.Fatalf("initialize smtp to submission server: %v", err)
|
|
}
|
|
defer client.Close()
|
|
|
|
// Send the message to the server, which will add it to its queue.
|
|
req8bitmime := false // ASCII-only, so 8bitmime not required.
|
|
reqSMTPUTF8 := false // No UTF-8 headers, so smtputf8 not required.
|
|
requireTLS := false // Not supported by most servers at the time of writing.
|
|
msg := "From: <[email protected]>\r\nTo: <[email protected]>\r\nSubject: hi\r\n\r\nnice to test you.\r\n"
|
|
err = client.Deliver(ctx, "[email protected]", "[email protected]", int64(len(msg)), strings.NewReader(msg), req8bitmime, reqSMTPUTF8, requireTLS)
|
|
if err != nil {
|
|
log.Fatalf("submit message to smtp server: %v", err)
|
|
}
|
|
|
|
// Message has been submitted.
|
|
}
|