merge v0.23.0-rc changes
This commit is contained in:
@@ -4,7 +4,7 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHtml2Text(t *testing.T) {
|
||||
func TestHTML2Text(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
html string
|
||||
expected string
|
||||
|
||||
@@ -3,6 +3,8 @@ package mailer
|
||||
import (
|
||||
"io"
|
||||
"net/mail"
|
||||
|
||||
"github.com/pocketbase/pocketbase/tools/hook"
|
||||
)
|
||||
|
||||
// Message defines a generic email message struct.
|
||||
@@ -24,6 +26,16 @@ type Mailer interface {
|
||||
Send(message *Message) error
|
||||
}
|
||||
|
||||
// SendInterceptor is optional interface for registering mail send hooks.
|
||||
type SendInterceptor interface {
|
||||
OnSend() *hook.Hook[*SendEvent]
|
||||
}
|
||||
|
||||
type SendEvent struct {
|
||||
hook.Event
|
||||
Message *Message
|
||||
}
|
||||
|
||||
// addressesToStrings converts the provided address to a list of serialized RFC 5322 strings.
|
||||
//
|
||||
// To export only the email part of mail.Address, you can set withName to false.
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/pocketbase/pocketbase/tools/hook"
|
||||
)
|
||||
|
||||
var _ Mailer = (*Sendmail)(nil)
|
||||
@@ -16,10 +18,29 @@ var _ Mailer = (*Sendmail)(nil)
|
||||
//
|
||||
// This client is usually recommended only for development and testing.
|
||||
type Sendmail struct {
|
||||
onSend *hook.Hook[*SendEvent]
|
||||
}
|
||||
|
||||
// Send implements `mailer.Mailer` interface.
|
||||
// OnSend implements [mailer.SendInterceptor] interface.
|
||||
func (c *Sendmail) OnSend() *hook.Hook[*SendEvent] {
|
||||
if c.onSend == nil {
|
||||
c.onSend = &hook.Hook[*SendEvent]{}
|
||||
}
|
||||
return c.onSend
|
||||
}
|
||||
|
||||
// Send implements [mailer.Mailer] interface.
|
||||
func (c *Sendmail) Send(m *Message) error {
|
||||
if c.onSend != nil {
|
||||
return c.onSend.Trigger(&SendEvent{Message: m}, func(e *SendEvent) error {
|
||||
return c.send(e.Message)
|
||||
})
|
||||
}
|
||||
|
||||
return c.send(m)
|
||||
}
|
||||
|
||||
func (c *Sendmail) send(m *Message) error {
|
||||
toAddresses := addressesToStrings(m.To, false)
|
||||
|
||||
headers := make(http.Header)
|
||||
@@ -74,5 +95,5 @@ func findSendmailPath() (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return "", errors.New("failed to locate a sendmail executable path")
|
||||
return "", errors.New("Failed to locate a sendmail executable path.")
|
||||
}
|
||||
|
||||
+32
-30
@@ -7,43 +7,27 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/domodwyer/mailyak/v3"
|
||||
"github.com/pocketbase/pocketbase/tools/hook"
|
||||
"github.com/pocketbase/pocketbase/tools/security"
|
||||
)
|
||||
|
||||
var _ Mailer = (*SmtpClient)(nil)
|
||||
var _ Mailer = (*SMTPClient)(nil)
|
||||
|
||||
const (
|
||||
SmtpAuthPlain = "PLAIN"
|
||||
SmtpAuthLogin = "LOGIN"
|
||||
SMTPAuthPlain = "PLAIN"
|
||||
SMTPAuthLogin = "LOGIN"
|
||||
)
|
||||
|
||||
// Deprecated: Use directly the SmtpClient struct literal.
|
||||
//
|
||||
// NewSmtpClient creates new SmtpClient with the provided configuration.
|
||||
func NewSmtpClient(
|
||||
host string,
|
||||
port int,
|
||||
username string,
|
||||
password string,
|
||||
tls bool,
|
||||
) *SmtpClient {
|
||||
return &SmtpClient{
|
||||
Host: host,
|
||||
Port: port,
|
||||
Username: username,
|
||||
Password: password,
|
||||
Tls: tls,
|
||||
}
|
||||
}
|
||||
|
||||
// SmtpClient defines a SMTP mail client structure that implements
|
||||
// SMTPClient defines a SMTP mail client structure that implements
|
||||
// `mailer.Mailer` interface.
|
||||
type SmtpClient struct {
|
||||
Host string
|
||||
type SMTPClient struct {
|
||||
onSend *hook.Hook[*SendEvent]
|
||||
|
||||
TLS bool
|
||||
Port int
|
||||
Host string
|
||||
Username string
|
||||
Password string
|
||||
Tls bool
|
||||
|
||||
// SMTP auth method to use
|
||||
// (if not explicitly set, defaults to "PLAIN")
|
||||
@@ -56,12 +40,30 @@ type SmtpClient struct {
|
||||
LocalName string
|
||||
}
|
||||
|
||||
// Send implements `mailer.Mailer` interface.
|
||||
func (c *SmtpClient) Send(m *Message) error {
|
||||
// OnSend implements [mailer.SendInterceptor] interface.
|
||||
func (c *SMTPClient) OnSend() *hook.Hook[*SendEvent] {
|
||||
if c.onSend == nil {
|
||||
c.onSend = &hook.Hook[*SendEvent]{}
|
||||
}
|
||||
return c.onSend
|
||||
}
|
||||
|
||||
// Send implements [mailer.Mailer] interface.
|
||||
func (c *SMTPClient) Send(m *Message) error {
|
||||
if c.onSend != nil {
|
||||
return c.onSend.Trigger(&SendEvent{Message: m}, func(e *SendEvent) error {
|
||||
return c.send(e.Message)
|
||||
})
|
||||
}
|
||||
|
||||
return c.send(m)
|
||||
}
|
||||
|
||||
func (c *SMTPClient) send(m *Message) error {
|
||||
var smtpAuth smtp.Auth
|
||||
if c.Username != "" || c.Password != "" {
|
||||
switch c.AuthMethod {
|
||||
case SmtpAuthLogin:
|
||||
case SMTPAuthLogin:
|
||||
smtpAuth = &smtpLoginAuth{c.Username, c.Password}
|
||||
default:
|
||||
smtpAuth = smtp.PlainAuth("", c.Username, c.Password, c.Host)
|
||||
@@ -70,7 +72,7 @@ func (c *SmtpClient) Send(m *Message) error {
|
||||
|
||||
// create mail instance
|
||||
var yak *mailyak.MailYak
|
||||
if c.Tls {
|
||||
if c.TLS {
|
||||
var tlsErr error
|
||||
yak, tlsErr = mailyak.NewWithTLS(fmt.Sprintf("%s:%d", c.Host, c.Port), smtpAuth, nil)
|
||||
if tlsErr != nil {
|
||||
|
||||
+16
-14
@@ -56,24 +56,26 @@ func TestLoginAuthStart(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, s := range scenarios {
|
||||
method, resp, err := auth.Start(s.serverInfo)
|
||||
t.Run(s.name, func(t *testing.T) {
|
||||
method, resp, err := auth.Start(s.serverInfo)
|
||||
|
||||
hasErr := err != nil
|
||||
if hasErr != s.expectError {
|
||||
t.Fatalf("[%s] Expected hasErr %v, got %v", s.name, s.expectError, hasErr)
|
||||
}
|
||||
hasErr := err != nil
|
||||
if hasErr != s.expectError {
|
||||
t.Fatalf("Expected hasErr %v, got %v", s.expectError, hasErr)
|
||||
}
|
||||
|
||||
if hasErr {
|
||||
continue
|
||||
}
|
||||
if hasErr {
|
||||
return
|
||||
}
|
||||
|
||||
if len(resp) != 0 {
|
||||
t.Fatalf("[%s] Expected empty data response, got %v", s.name, resp)
|
||||
}
|
||||
if len(resp) != 0 {
|
||||
t.Fatalf("Expected empty data response, got %v", resp)
|
||||
}
|
||||
|
||||
if method != "LOGIN" {
|
||||
t.Fatalf("[%s] Expected LOGIN, got %v", s.name, method)
|
||||
}
|
||||
if method != "LOGIN" {
|
||||
t.Fatalf("Expected LOGIN, got %v", method)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user