smtp/examples/main.go

57 lines
1.5 KiB
Go
Raw Permalink Normal View History

2024-11-21 19:35:30 +00:00
package main
import (
"fmt"
"net"
2024-11-25 15:53:40 +00:00
"net/textproto"
"git.ailur.dev/ailur/smtp"
2024-11-21 19:35:30 +00:00
)
// DatabaseBackend is a smtp.DatabaseBackend implementation that always returns true for CheckUser and prints the mail data to stdout.
var DatabaseBackend = smtp.DatabaseBackend{
CheckUser: func(address *smtp.Address) (bool, error) {
return true, nil
},
2024-11-25 15:53:40 +00:00
WriteMail: func(mail *smtp.Mail) error {
2024-11-21 19:35:30 +00:00
fmt.Println(string(mail.Data))
2024-11-25 15:53:40 +00:00
return nil
2024-11-21 19:35:30 +00:00
},
}
// AuthenticationBackend is a smtp.AuthenticationBackend implementation that always returns a fixed address for Authenticate.
var AuthenticationBackend = smtp.AuthenticationBackend{
Authenticate: func(initial string, conn *textproto.Conn) (smtp.CheckAddress, error) {
return func(address *smtp.Address) (bool, error) {
return true, nil
2024-11-21 19:35:30 +00:00
}, nil
},
}
func main() {
go func() {
// Serve on the server-to-server port
listener, err := net.Listen("tcp", ":25")
if err != nil {
panic(err)
}
receiver := smtp.NewReceiver(listener, "localhost", []string{"localhost", "127.0.0.1", "0.0.0.0", "example.org", "192.168.1.253"}, false, DatabaseBackend, AuthenticationBackend, nil)
err = receiver.Serve()
panic(err)
}()
go func() {
// Serve on the submission port
listener, err := net.Listen("tcp", ":587")
if err != nil {
panic(err)
}
receiver := smtp.NewReceiver(listener, "localhost", []string{"localhost", "127.0.0.1", "0.0.0.0", "cta.social"}, false, DatabaseBackend, AuthenticationBackend, nil)
err = receiver.Serve()
panic(err)
}()
// Block forever
select {}
}