mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-08 23:12:54 +01:00
Added AddTo() and FromFormat()
This commit is contained in:
parent
6bc9832ede
commit
7266155cf4
2 changed files with 94 additions and 0 deletions
16
msg.go
16
msg.go
|
@ -133,11 +133,27 @@ func (m *Msg) From(f string) error {
|
|||
return m.SetAddrHeader(HeaderFrom, f)
|
||||
}
|
||||
|
||||
// FromFormat takes a name and address, formats them RFC5322 compliant and stores them as
|
||||
// the From address header field
|
||||
func (m *Msg) FromFormat(n, a string) error {
|
||||
return m.SetAddrHeader(HeaderFrom, fmt.Sprintf(`"%s" <%s>`, n, a))
|
||||
}
|
||||
|
||||
// To takes and validates a given mail address list sets the To: addresses of the Msg
|
||||
func (m *Msg) To(t ...string) error {
|
||||
return m.SetAddrHeader(HeaderTo, t...)
|
||||
}
|
||||
|
||||
// AddTo adds an additional address to the To address header field
|
||||
func (m *Msg) AddTo(t string) error {
|
||||
var tl []string
|
||||
for _, ct := range m.addrHeader[HeaderTo] {
|
||||
tl = append(tl, ct.String())
|
||||
}
|
||||
tl = append(tl, t)
|
||||
return m.To(tl...)
|
||||
}
|
||||
|
||||
// Subject sets the "Subject" header field of the Msg
|
||||
func (m *Msg) Subject(s string) {
|
||||
m.SetHeader(HeaderSubject, s)
|
||||
|
|
78
msg_test.go
Normal file
78
msg_test.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
package mail
|
||||
|
||||
import (
|
||||
"net/mail"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestMsg_AddTo tests the AddTo() method for the Msg object
|
||||
func TestMsg_AddTo(t *testing.T) {
|
||||
a := []string{"address1@example.com", "address2@example.com"}
|
||||
na := "address3@example.com"
|
||||
m := NewMsg()
|
||||
if err := m.To(a...); err != nil {
|
||||
t.Errorf("failed to set TO addresses: %s", err)
|
||||
return
|
||||
}
|
||||
if err := m.AddTo(na); err != nil {
|
||||
t.Errorf("AddTo failed: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
atf := false
|
||||
for _, v := range m.addrHeader[HeaderTo] {
|
||||
if v.Address == na {
|
||||
atf = true
|
||||
}
|
||||
}
|
||||
if !atf {
|
||||
t.Errorf("AddTo() failed. Address %q not found in TO address slice.", na)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMsg_FromFormat tests the FromFormat() method for the Msg object
|
||||
func TestMsg_FromFormat(t *testing.T) {
|
||||
tests := []struct {
|
||||
tname string
|
||||
name string
|
||||
addr string
|
||||
want string
|
||||
fail bool
|
||||
}{
|
||||
{"valid name and addr", "Toni Tester", "tester@example.com",
|
||||
`"Toni Tester" <tester@example.com>`, false},
|
||||
{"no name with valid addr", "", "tester@example.com",
|
||||
`<tester@example.com>`, false},
|
||||
{"valid name with invalid addr", "Toni Tester", "@example.com",
|
||||
``, true},
|
||||
}
|
||||
|
||||
m := NewMsg()
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.tname, func(t *testing.T) {
|
||||
if err := m.FromFormat(tt.name, tt.addr); err != nil && !tt.fail {
|
||||
t.Errorf("failed to FromFormat(): %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
var fa *mail.Address
|
||||
f, ok := m.addrHeader[HeaderFrom]
|
||||
if ok && len(f) > 0 {
|
||||
fa = f[0]
|
||||
}
|
||||
if (!ok || len(f) == 0) && !tt.fail {
|
||||
t.Errorf(`valid from address expected, but "From:" field is empty`)
|
||||
return
|
||||
}
|
||||
if tt.fail && len(f) > 0 {
|
||||
t.Errorf("FromFormat() was supposed to failed but got value: %s", fa.String())
|
||||
return
|
||||
}
|
||||
|
||||
if !tt.fail && fa.String() != tt.want {
|
||||
t.Errorf("wrong result for FromFormat(). Want: %s, got: %s", tt.want, fa.String())
|
||||
}
|
||||
m.addrHeader[HeaderFrom] = nil
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue