diff --git a/builder.go b/builder.go index fe643116..fe9763fc 100644 --- a/builder.go +++ b/builder.go @@ -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 @@ -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. @@ -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() { diff --git a/builder_test.go b/builder_test.go index 437b0c23..e6e60dcf 100644 --- a/builder_test.go +++ b/builder_test.go @@ -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\" " @@ -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\" , \"two\" " + 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) {