You've already forked pocketbase
mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-11-06 17:39:57 +02:00
[#1728] normalized mailer.Message recipient fields
This commit is contained in:
@@ -8,9 +8,9 @@ import (
|
||||
// Message defines a generic email message struct.
|
||||
type Message struct {
|
||||
From mail.Address
|
||||
To mail.Address
|
||||
Bcc []string
|
||||
Cc []string
|
||||
To []mail.Address
|
||||
Bcc []mail.Address
|
||||
Cc []mail.Address
|
||||
Subject string
|
||||
HTML string
|
||||
Text string
|
||||
@@ -23,3 +23,21 @@ type Mailer interface {
|
||||
// Send sends an email with the provided Message.
|
||||
Send(message *Message) error
|
||||
}
|
||||
|
||||
// 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.
|
||||
func addressesToStrings(addresses []mail.Address, withName bool) []string {
|
||||
result := make([]string, len(addresses))
|
||||
|
||||
for i, addr := range addresses {
|
||||
if withName && addr.Name != "" {
|
||||
result[i] = addr.String()
|
||||
} else {
|
||||
// keep only the email part to avoid wrapping in angle-brackets
|
||||
result[i] = addr.Address
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"mime"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var _ Mailer = (*Sendmail)(nil)
|
||||
@@ -19,11 +20,13 @@ type Sendmail struct {
|
||||
|
||||
// Send implements `mailer.Mailer` interface.
|
||||
func (c *Sendmail) Send(m *Message) error {
|
||||
toAddresses := addressesToStrings(m.To, false)
|
||||
|
||||
headers := make(http.Header)
|
||||
headers.Set("Subject", mime.QEncoding.Encode("utf-8", m.Subject))
|
||||
headers.Set("From", m.From.String())
|
||||
headers.Set("To", m.To.String())
|
||||
headers.Set("Content-Type", "text/html; charset=UTF-8")
|
||||
headers.Set("To", strings.Join(toAddresses, ","))
|
||||
|
||||
cmdPath, err := findSendmailPath()
|
||||
if err != nil {
|
||||
@@ -51,7 +54,7 @@ func (c *Sendmail) Send(m *Message) error {
|
||||
}
|
||||
// ---
|
||||
|
||||
sendmail := exec.Command(cmdPath, m.To.Address)
|
||||
sendmail := exec.Command(cmdPath, strings.Join(toAddresses, ","))
|
||||
sendmail.Stdin = &buffer
|
||||
|
||||
return sendmail.Run()
|
||||
|
||||
@@ -75,7 +75,6 @@ func (c *SmtpClient) Send(m *Message) error {
|
||||
yak.FromName(m.From.Name)
|
||||
}
|
||||
yak.From(m.From.Address)
|
||||
yak.To(m.To.Address)
|
||||
yak.Subject(m.Subject)
|
||||
yak.HTML().Set(m.HTML)
|
||||
|
||||
@@ -88,12 +87,16 @@ func (c *SmtpClient) Send(m *Message) error {
|
||||
yak.Plain().Set(m.Text)
|
||||
}
|
||||
|
||||
if len(m.To) > 0 {
|
||||
yak.To(addressesToStrings(m.To, true)...)
|
||||
}
|
||||
|
||||
if len(m.Bcc) > 0 {
|
||||
yak.Bcc(m.Bcc...)
|
||||
yak.Bcc(addressesToStrings(m.Bcc, true)...)
|
||||
}
|
||||
|
||||
if len(m.Cc) > 0 {
|
||||
yak.Cc(m.Cc...)
|
||||
yak.Cc(addressesToStrings(m.Cc, true)...)
|
||||
}
|
||||
|
||||
// add attachements (if any)
|
||||
|
||||
Reference in New Issue
Block a user