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

Add new methods and tests for handling email addresses #155

Merged
merged 2 commits into from
Nov 10, 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
19 changes: 19 additions & 0 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
tt "text/template"
"time"
Expand Down Expand Up @@ -352,6 +353,12 @@ func (m *Msg) ToIgnoreInvalid(t ...string) {
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
func (m *Msg) Cc(c ...string) error {
return m.SetAddrHeader(HeaderCc, c...)
Expand All @@ -374,6 +381,12 @@ func (m *Msg) CcIgnoreInvalid(c ...string) {
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
func (m *Msg) Bcc(b ...string) error {
return m.SetAddrHeader(HeaderBcc, b...)
Expand All @@ -396,6 +409,12 @@ func (m *Msg) BccIgnoreInvalid(b ...string) {
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
func (m *Msg) ReplyTo(r string) error {
rt, err := mail.ParseAddress(r)
Expand Down
177 changes: 177 additions & 0 deletions msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2937,3 +2937,180 @@ func TestMsg_EmbedReadSeeker(t *testing.T) {
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", "[email protected]", []*mail.Address{
{Name: "", Address: "[email protected]"},
}, false},
{
"valid multiple addresses", "[email protected],[email protected]",
[]*mail.Address{
{Name: "", Address: "[email protected]"},
{Name: "", Address: "[email protected]"},
},
false,
},
{
"valid multiple addresses with space and name",
`[email protected], "Toni Tester" <[email protected]>`,
[]*mail.Address{
{Name: "", Address: "[email protected]"},
{Name: "Toni Tester", Address: "[email protected]"},
},
false,
},
{
"invalid and valid multiple addresses", "[email protected],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", "[email protected]", []*mail.Address{
{Name: "", Address: "[email protected]"},
}, false},
{
"valid multiple addresses", "[email protected],[email protected]",
[]*mail.Address{
{Name: "", Address: "[email protected]"},
{Name: "", Address: "[email protected]"},
},
false,
},
{
"valid multiple addresses with space and name",
`[email protected], "Toni Tester" <[email protected]>`,
[]*mail.Address{
{Name: "", Address: "[email protected]"},
{Name: "Toni Tester", Address: "[email protected]"},
},
false,
},
{
"invalid and valid multiple addresses", "[email protected],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", "[email protected]", []*mail.Address{
{Name: "", Address: "[email protected]"},
}, false},
{
"valid multiple addresses", "[email protected],[email protected]",
[]*mail.Address{
{Name: "", Address: "[email protected]"},
{Name: "", Address: "[email protected]"},
},
false,
},
{
"valid multiple addresses with space and name",
`[email protected], "Toni Tester" <[email protected]>`,
[]*mail.Address{
{Name: "", Address: "[email protected]"},
{Name: "Toni Tester", Address: "[email protected]"},
},
false,
},
{
"invalid and valid multiple addresses", "[email protected],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