fixed the notification service error when the user's display name contained special characters
This commit is contained in:
@@ -4,7 +4,7 @@ package channels
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
stdmail "net/mail"
|
||||
"strings"
|
||||
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/log"
|
||||
@@ -31,16 +31,23 @@ type Message struct {
|
||||
|
||||
// NewMailChannel instantiates a new mail communication channel.
|
||||
func NewMailChannel(cfg config.Config, logger log.Logger) (Channel, error) {
|
||||
a, err := stdmail.ParseAddress(cfg.Notifications.SMTP.Sender)
|
||||
if err != nil {
|
||||
logger.Err(err).Msg("parsing error, the 'smtp_sender' must be a valid single RFC 5322 address.")
|
||||
return nil, err
|
||||
}
|
||||
return Mail{
|
||||
conf: cfg,
|
||||
logger: logger,
|
||||
conf: cfg,
|
||||
smtpAddress: *a,
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Mail is the communication channel for email.
|
||||
type Mail struct {
|
||||
conf config.Config
|
||||
logger log.Logger
|
||||
conf config.Config
|
||||
smtpAddress stdmail.Address
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
func (m Mail) getMailClient() (*mail.SMTPClient, error) {
|
||||
@@ -113,11 +120,7 @@ func (m Mail) SendMessage(ctx context.Context, message *Message) error {
|
||||
}
|
||||
|
||||
email := mail.NewMSG()
|
||||
if message.Sender != "" {
|
||||
email.SetFrom(fmt.Sprintf("%s via %s", message.Sender, m.conf.Notifications.SMTP.Sender)).AddTo(message.Recipient...)
|
||||
} else {
|
||||
email.SetFrom(m.conf.Notifications.SMTP.Sender).AddTo(message.Recipient...)
|
||||
}
|
||||
email.SetFrom(appendSender(message.Sender, m.smtpAddress)).AddTo(message.Recipient...)
|
||||
email.SetSubject(message.Subject)
|
||||
email.SetBody(mail.TextPlain, message.TextBody)
|
||||
if message.HTMLBody != "" {
|
||||
@@ -129,3 +132,10 @@ func (m Mail) SendMessage(ctx context.Context, message *Message) error {
|
||||
|
||||
return email.Send(smtpClient)
|
||||
}
|
||||
|
||||
func appendSender(sender string, a stdmail.Address) string {
|
||||
if strings.TrimSpace(sender) != "" {
|
||||
a.Name = strings.TrimSpace(sender + " via " + a.Name)
|
||||
}
|
||||
return a.String()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package channels
|
||||
|
||||
import (
|
||||
"net/mail"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_appendSender(t *testing.T) {
|
||||
type args struct {
|
||||
sender string
|
||||
a mail.Address
|
||||
}
|
||||
|
||||
a1, err := mail.ParseAddress("ownCloud <noreply@example.com>")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
a2, err := mail.ParseAddress("noreply@example.com")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
sender string
|
||||
want1 string
|
||||
want2 string
|
||||
}{
|
||||
{
|
||||
name: "empty sender",
|
||||
sender: "",
|
||||
want1: `"ownCloud" <noreply@example.com>`,
|
||||
want2: `<noreply@example.com>`,
|
||||
},
|
||||
{
|
||||
name: "not empty sender",
|
||||
sender: `Joe Q. Public`,
|
||||
want1: `"Joe Q. Public via ownCloud" <noreply@example.com>`,
|
||||
want2: `"Joe Q. Public via" <noreply@example.com>`,
|
||||
},
|
||||
{
|
||||
name: "sender whit comma and semicolon",
|
||||
sender: `Joe, Q; Public:`,
|
||||
want1: `"Joe, Q; Public: via ownCloud" <noreply@example.com>`,
|
||||
want2: `"Joe, Q; Public: via" <noreply@example.com>`,
|
||||
},
|
||||
{
|
||||
name: "sender with quotes",
|
||||
sender: `Joe Q. "Public"`,
|
||||
want1: `"Joe Q. \"Public\" via ownCloud" <noreply@example.com>`,
|
||||
want2: `"Joe Q. \"Public\" via" <noreply@example.com>`,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := appendSender(tt.sender, *a1); got != tt.want1 {
|
||||
t.Errorf("appendSender() = %v, want %v", got, tt.want1)
|
||||
}
|
||||
if got := appendSender(tt.sender, *a2); got != tt.want2 {
|
||||
t.Errorf("appendSender() = %v, want %v", got, tt.want2)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package parser
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/mail"
|
||||
"strings"
|
||||
|
||||
ociscfg "github.com/owncloud/ocis/v2/ocis-pkg/config"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/shared"
|
||||
@@ -54,6 +56,17 @@ func Validate(cfg *config.Config) error {
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
if strings.TrimSpace(cfg.Notifications.SMTP.Sender) == "" {
|
||||
return fmt.Errorf("the 'smtp_sender' must be a valid single RFC 5322 address. parsing error: the address cannot be empty")
|
||||
}
|
||||
if s, err := mail.ParseAddress(cfg.Notifications.SMTP.Sender); err == nil {
|
||||
cfg.Notifications.SMTP.Sender = s.String()
|
||||
} else {
|
||||
return fmt.Errorf("the 'smtp_sender' must be a valid single RFC 5322 address. parsing error %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.ServiceAccount.ServiceAccountID == "" {
|
||||
return shared.MissingServiceAccountID(cfg.Service.Name)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user