mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-22 13:50:49 +01:00
#152: Add new methods and tests for handling email addresses
Added new methods `ToFromString()`, `CcFromString()` and `BccFromString()` in msg.go file to handle strings of comma-separated email addresses.
This commit is contained in:
parent
cf45f64cc5
commit
cc14864129
2 changed files with 196 additions and 0 deletions
19
msg.go
19
msg.go
|
@ -17,6 +17,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
tt "text/template"
|
tt "text/template"
|
||||||
"time"
|
"time"
|
||||||
|
@ -352,6 +353,12 @@ func (m *Msg) ToIgnoreInvalid(t ...string) {
|
||||||
m.SetAddrHeaderIgnoreInvalid(HeaderTo, t...)
|
m.SetAddrHeaderIgnoreInvalid(HeaderTo, t...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToFromString takes and validates a given string of comma separted
|
||||||
|
// mail address and sets them as To: addresses of the Msg
|
||||||
|
func (m *Msg) ToFromString(v string) error {
|
||||||
|
return m.To(strings.Split(v, ",")...)
|
||||||
|
}
|
||||||
|
|
||||||
// Cc takes and validates a given mail address list sets the Cc: addresses of the Msg
|
// Cc takes and validates a given mail address list sets the Cc: addresses of the Msg
|
||||||
func (m *Msg) Cc(c ...string) error {
|
func (m *Msg) Cc(c ...string) error {
|
||||||
return m.SetAddrHeader(HeaderCc, c...)
|
return m.SetAddrHeader(HeaderCc, c...)
|
||||||
|
@ -374,6 +381,12 @@ func (m *Msg) CcIgnoreInvalid(c ...string) {
|
||||||
m.SetAddrHeaderIgnoreInvalid(HeaderCc, c...)
|
m.SetAddrHeaderIgnoreInvalid(HeaderCc, c...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CcFromString takes and validates a given string of comma separted
|
||||||
|
// mail address and sets them as Cc: addresses of the Msg
|
||||||
|
func (m *Msg) CcFromString(v string) error {
|
||||||
|
return m.Cc(strings.Split(v, ",")...)
|
||||||
|
}
|
||||||
|
|
||||||
// Bcc takes and validates a given mail address list sets the Bcc: addresses of the Msg
|
// Bcc takes and validates a given mail address list sets the Bcc: addresses of the Msg
|
||||||
func (m *Msg) Bcc(b ...string) error {
|
func (m *Msg) Bcc(b ...string) error {
|
||||||
return m.SetAddrHeader(HeaderBcc, b...)
|
return m.SetAddrHeader(HeaderBcc, b...)
|
||||||
|
@ -396,6 +409,12 @@ func (m *Msg) BccIgnoreInvalid(b ...string) {
|
||||||
m.SetAddrHeaderIgnoreInvalid(HeaderBcc, b...)
|
m.SetAddrHeaderIgnoreInvalid(HeaderBcc, b...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BccFromString takes and validates a given string of comma separted
|
||||||
|
// mail address and sets them as Bcc: addresses of the Msg
|
||||||
|
func (m *Msg) BccFromString(v string) error {
|
||||||
|
return m.Bcc(strings.Split(v, ",")...)
|
||||||
|
}
|
||||||
|
|
||||||
// ReplyTo takes and validates a given mail address and sets it as "Reply-To" addrHeader of the Msg
|
// ReplyTo takes and validates a given mail address and sets it as "Reply-To" addrHeader of the Msg
|
||||||
func (m *Msg) ReplyTo(r string) error {
|
func (m *Msg) ReplyTo(r string) error {
|
||||||
rt, err := mail.ParseAddress(r)
|
rt, err := mail.ParseAddress(r)
|
||||||
|
|
177
msg_test.go
177
msg_test.go
|
@ -2937,3 +2937,180 @@ func TestMsg_EmbedReadSeeker(t *testing.T) {
|
||||||
t.Errorf("EmbedReadSeeker() failed. Expected string: %q, got: %q", ts, wbuf.String())
|
t.Errorf("EmbedReadSeeker() failed. Expected string: %q, got: %q", ts, wbuf.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestMsg_ToFromString tests Msg.ToFromString in different scenarios
|
||||||
|
func TestMsg_ToFromString(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
n string
|
||||||
|
v string
|
||||||
|
w []*mail.Address
|
||||||
|
sf bool
|
||||||
|
}{
|
||||||
|
{"valid single address", "test@test.com", []*mail.Address{
|
||||||
|
{"", "test@test.com"},
|
||||||
|
}, false},
|
||||||
|
{
|
||||||
|
"valid multiple addresses", "test@test.com,test2@example.com",
|
||||||
|
[]*mail.Address{
|
||||||
|
{"", "test@test.com"},
|
||||||
|
{"", "test2@example.com"},
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid multiple addresses with space and name",
|
||||||
|
`test@test.com, "Toni Tester" <test2@example.com>`,
|
||||||
|
[]*mail.Address{
|
||||||
|
{"", "test@test.com"},
|
||||||
|
{"Toni Tester", "test2@example.com"},
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"invalid and valid multiple addresses", "test@test.com,test2#example.com", nil,
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.n, func(t *testing.T) {
|
||||||
|
m := NewMsg()
|
||||||
|
if err := m.ToFromString(tt.v); err != nil && !tt.sf {
|
||||||
|
t.Errorf("Msg.ToFromString failed: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mto := m.GetTo()
|
||||||
|
if len(mto) != len(tt.w) {
|
||||||
|
t.Errorf("Msg.ToFromString failed, expected len: %d, got: %d", len(tt.w),
|
||||||
|
len(mto))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for i := range mto {
|
||||||
|
w := tt.w[i]
|
||||||
|
g := mto[i]
|
||||||
|
if w.String() != g.String() {
|
||||||
|
t.Errorf("Msg.ToFromString failed, expected address: %s, got: %s",
|
||||||
|
w.String(), g.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMsg_CcFromString tests Msg.CcFromString in different scenarios
|
||||||
|
func TestMsg_CcFromString(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
n string
|
||||||
|
v string
|
||||||
|
w []*mail.Address
|
||||||
|
sf bool
|
||||||
|
}{
|
||||||
|
{"valid single address", "test@test.com", []*mail.Address{
|
||||||
|
{"", "test@test.com"},
|
||||||
|
}, false},
|
||||||
|
{
|
||||||
|
"valid multiple addresses", "test@test.com,test2@example.com",
|
||||||
|
[]*mail.Address{
|
||||||
|
{"", "test@test.com"},
|
||||||
|
{"", "test2@example.com"},
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid multiple addresses with space and name",
|
||||||
|
`test@test.com, "Toni Tester" <test2@example.com>`,
|
||||||
|
[]*mail.Address{
|
||||||
|
{"", "test@test.com"},
|
||||||
|
{"Toni Tester", "test2@example.com"},
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"invalid and valid multiple addresses", "test@test.com,test2#example.com", nil,
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.n, func(t *testing.T) {
|
||||||
|
m := NewMsg()
|
||||||
|
if err := m.CcFromString(tt.v); err != nil && !tt.sf {
|
||||||
|
t.Errorf("Msg.CcFromString failed: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mto := m.GetCc()
|
||||||
|
if len(mto) != len(tt.w) {
|
||||||
|
t.Errorf("Msg.CcFromString failed, expected len: %d, got: %d", len(tt.w),
|
||||||
|
len(mto))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for i := range mto {
|
||||||
|
w := tt.w[i]
|
||||||
|
g := mto[i]
|
||||||
|
if w.String() != g.String() {
|
||||||
|
t.Errorf("Msg.CcFromString failed, expected address: %s, got: %s",
|
||||||
|
w.String(), g.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMsg_BccFromString tests Msg.BccFromString in different scenarios
|
||||||
|
func TestMsg_BccFromString(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
n string
|
||||||
|
v string
|
||||||
|
w []*mail.Address
|
||||||
|
sf bool
|
||||||
|
}{
|
||||||
|
{"valid single address", "test@test.com", []*mail.Address{
|
||||||
|
{"", "test@test.com"},
|
||||||
|
}, false},
|
||||||
|
{
|
||||||
|
"valid multiple addresses", "test@test.com,test2@example.com",
|
||||||
|
[]*mail.Address{
|
||||||
|
{"", "test@test.com"},
|
||||||
|
{"", "test2@example.com"},
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"valid multiple addresses with space and name",
|
||||||
|
`test@test.com, "Toni Tester" <test2@example.com>`,
|
||||||
|
[]*mail.Address{
|
||||||
|
{"", "test@test.com"},
|
||||||
|
{"Toni Tester", "test2@example.com"},
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"invalid and valid multiple addresses", "test@test.com,test2#example.com", nil,
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.n, func(t *testing.T) {
|
||||||
|
m := NewMsg()
|
||||||
|
if err := m.BccFromString(tt.v); err != nil && !tt.sf {
|
||||||
|
t.Errorf("Msg.BccFromString failed: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mto := m.GetBcc()
|
||||||
|
if len(mto) != len(tt.w) {
|
||||||
|
t.Errorf("Msg.BccFromString failed, expected len: %d, got: %d", len(tt.w),
|
||||||
|
len(mto))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for i := range mto {
|
||||||
|
w := tt.w[i]
|
||||||
|
g := mto[i]
|
||||||
|
if w.String() != g.String() {
|
||||||
|
t.Errorf("Msg.BccFromString failed, expected address: %s, got: %s",
|
||||||
|
w.String(), g.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue