Skip to content

Commit

Permalink
feat: add support for multiple Reply-To addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
nolotz committed Apr 15, 2023
1 parent 67b5cbe commit ac426eb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
32 changes: 26 additions & 6 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
type MailBuilder struct {
to, cc, bcc []mail.Address
from mail.Address
replyTo mail.Address
replyTo []mail.Address
subject string
date time.Time
header textproto.MIMEHeader
Expand Down Expand Up @@ -153,13 +153,33 @@ func (p *MailBuilder) GetBCC() []mail.Address {
// ReplyTo returns a copy of MailBuilder with this name & address appended to the To header. name
// may be empty.
func (p MailBuilder) ReplyTo(name, addr string) MailBuilder {
p.replyTo = mail.Address{Name: name, Address: addr}
if len(addr) > 0 {
p.replyTo = append(p.replyTo, mail.Address{Name: name, Address: addr})
}
return p
}

// ReplyToAddrs returns a copy of MailBuilder with the new reply to header list. This method only
// has an effect if the Send method is used to transmit the message, there is no effect on the parts
// returned by Build().
func (p MailBuilder) ReplyToAddrs(replyTo []mail.Address) MailBuilder {
p.replyTo = replyTo
return p
}

// GetReplyTo returns the stored replyTo header.
// GetReplyTo returns the first stored replyTo header.
func (p *MailBuilder) GetReplyTo() mail.Address {
return p.replyTo
if len(p.replyTo) > 0 {
return p.replyTo[0]
}
return mail.Address{}
}

// GetReplyToAddrs returns a copy of the stored replyTo header addresses.
func (p *MailBuilder) GetReplyToAddrs() []mail.Address {
var replyTo []mail.Address
copy(replyTo, p.replyTo)
return replyTo
}

// Header returns a copy of MailBuilder with the specified value added to the named header.
Expand Down Expand Up @@ -386,8 +406,8 @@ func (p MailBuilder) Build() (*Part, error) {
if len(p.cc) > 0 {
h.Set("Cc", stringutil.JoinAddress(p.cc))
}
if p.replyTo.Address != "" {
h.Set("Reply-To", p.replyTo.String())
if len(p.replyTo) > 0 {
h.Set("Reply-To", stringutil.JoinAddress(p.replyTo))
}
date := p.date
if date.IsZero() {
Expand Down
21 changes: 21 additions & 0 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,12 @@ func TestBuilderReplyTo(t *testing.T) {
t.Error("ReplyTo() should not mutate receiver, failed")
}

a = enmime.Builder().ReplyToAddrs([]mail.Address{{Name: "name", Address: "foo"}})
b = a.ReplyToAddrs([]mail.Address{{Name: "name", Address: "bar"}})
if a.Equals(b) {
t.Error("ReplyToAddrs() should not mutate receiver, failed")
}

a = enmime.Builder().ToAddrs(addrSlice).From("name", "foo").Subject("foo")
a = a.ReplyTo("one", "one@inbucket.org")
want := "\"one\" <one@inbucket.org>"
Expand All @@ -371,6 +377,21 @@ func TestBuilderReplyTo(t *testing.T) {
if got != want {
t.Errorf("Reply-To: %q, want: %q", got, want)
}

input := []mail.Address{
{Name: "one", Address: "one@inbucket.org"},
{Name: "two", Address: "two@inbucket.org"},
}
a = enmime.Builder().ReplyToAddrs(input).ToAddrs(input).From("name", "foo").Subject("foo")
want = "\"one\" <one@inbucket.org>, \"two\" <two@inbucket.org>"
p, err = a.Build()
if err != nil {
t.Fatal(err)
}
got = p.Header.Get("Reply-To")
if got != want {
t.Errorf("Reply-To List: %q, want: %q", got, want)
}
}

func TestBuilderText(t *testing.T) {
Expand Down

0 comments on commit ac426eb

Please sign in to comment.