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

Allows users to submit rfc822 formatted email addresses #194

Merged
merged 2 commits into from
Oct 23, 2018
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
11 changes: 11 additions & 0 deletions helpers/mail/mail_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mail
import (
"encoding/json"
"log"
"net/mail"
)

type SGMailV3 struct {
Expand Down Expand Up @@ -569,3 +570,13 @@ func NewSandboxModeSetting(enable bool, forwardSpam bool, spamCheck *SpamCheckSe
SpamCheck: spamCheck,
}
}

// ParseEmail parses a string that contains an rfc822 formatted email address
// and returns an instance of *Email.
func ParseEmail(emailInfo string) (*Email, error) {
e, err := mail.ParseAddress(emailInfo)
if err != nil {
return nil, err
}
return NewEmail(e.Name, e.Address), nil
}
22 changes: 22 additions & 0 deletions helpers/mail/mail_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,3 +963,25 @@ func TestV3NewSandboxModeSetting(t *testing.T) {
t.Error("SpamCheck should not be nil")
}
}

func TestParseEmail(t *testing.T) {
e, err := ParseEmail("example example <[email protected]>")
if err != nil {
t.Error("Email should have been parsed successfully")
}
expectedName := "example example"
if e.Name != expectedName {
t.Errorf("Expect email with name %s but got %s", expectedName, e.Name)
}
expectedAddress := "[email protected]"
if e.Address != expectedAddress {
t.Errorf("Expect email with address %s but got %s", expectedAddress, e.Address)
}
}

func TestParseInvalidEmail(t *testing.T) {
_, err := ParseEmail("example example <example/example.com>")
if err == nil {
t.Error("Expected an error to be thrown from ParseEmail")
}
}