Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for multiple Reply-To addresses #288

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 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,26 @@ 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.
func (p *MailBuilder) GetReplyTo() mail.Address {
return p.replyTo
// GetReplyTo returns a copy of the stored replyTo header addresses.
func (p *MailBuilder) GetReplyTo() []mail.Address {
replyTo := make([]mail.Address, len(p.replyTo))
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 +399,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
27 changes: 27 additions & 0 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,12 @@ func TestBuilderReplyTo(t *testing.T) {
t.Error("Same ReplyTo(value) should be equal")
}

if !reflect.DeepEqual(a.GetReplyTo(), b.GetReplyTo()) {
t.Error("Same GetReplyTo() should be equal")
} else if len(a.GetReplyTo()) == 0 {
t.Error("GetReplyTo() shouldn't be empty")
}

a = enmime.Builder().ReplyTo("name", "foo")
b = enmime.Builder().ReplyTo("name", "bar")
if a.Equals(b) {
Expand All @@ -360,6 +366,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 +383,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: %q, want: %q", got, want)
}
}

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