diff --git a/models/group.go b/models/group.go index 6f53698a..b1b1c8fc 100644 --- a/models/group.go +++ b/models/group.go @@ -2,6 +2,7 @@ package models import ( "errors" + "fmt" "net/mail" "time" @@ -50,6 +51,19 @@ type Target struct { Position string `json:"position"` } +// Returns the email address to use in the "To" header of the email +func (t *Target) FormatAddress() string { + addr := t.Email + if t.FirstName != "" && t.LastName != "" { + a := &mail.Address{ + Name: fmt.Sprintf("%s %s", t.FirstName, t.LastName), + Address: t.Email, + } + addr = a.String() + } + return addr +} + // ErrNoEmailSpecified is thrown when no email is specified for the Target var ErrEmailNotSpecified = errors.New("No email address specified") diff --git a/models/models_test.go b/models/models_test.go index 7dfe7b24..5a5008b2 100644 --- a/models/models_test.go +++ b/models/models_test.go @@ -1,6 +1,7 @@ package models import ( + "net/mail" "regexp" "strings" "testing" @@ -348,3 +349,21 @@ func (s *ModelsSuite) TestGenerateResultId(c *check.C) { c.Assert(err, check.Equals, nil) c.Assert(match, check.Equals, true) } + +func (s *ModelsSuite) TestFormatAddress(c *check.C) { + r := Result{ + FirstName: "John", + LastName: "Doe", + Email: "johndoe@example.com", + } + expected := &mail.Address{ + Name: "John Doe", + Address: "johndoe@example.com", + } + c.Assert(r.FormatAddress(), check.Equals, expected.String()) + + r = Result{ + Email: "johndoe@example.com", + } + c.Assert(r.FormatAddress(), check.Equals, r.Email) +} diff --git a/models/result.go b/models/result.go index 6e8e9301..5fc4c565 100644 --- a/models/result.go +++ b/models/result.go @@ -2,9 +2,11 @@ package models import ( "crypto/rand" + "fmt" "log" "math/big" "net" + "net/mail" "github.com/jinzhu/gorm" "github.com/oschwald/maxminddb-golang" @@ -88,6 +90,19 @@ func (r *Result) GenerateId() error { return nil } +// Returns the email address to use in the "To" header of the email +func (r *Result) FormatAddress() string { + addr := r.Email + if r.FirstName != "" && r.LastName != "" { + a := &mail.Address{ + Name: fmt.Sprintf("%s %s", r.FirstName, r.LastName), + Address: r.Email, + } + addr = a.String() + } + return addr +} + // GetResult returns the Result object from the database // given the ResultId func GetResult(rid string) (Result, error) { diff --git a/worker/worker.go b/worker/worker.go index dfb0eae6..f3e7f3c7 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -168,7 +168,7 @@ func processCampaign(c *models.Campaign) { } e.SetHeader("Subject", subjBuff.String()) Logger.Println("Creating email using template") - e.SetHeader("To", t.Email) + e.SetHeader("To", t.FormatAddress()) if c.Template.Text != "" { var textBuff bytes.Buffer tmpl, err = template.New("text_template").Parse(c.Template.Text) @@ -302,7 +302,7 @@ func SendTestEmail(s *models.SendTestEmailRequest) error { e.SetHeader(parsedHeader.Key.String(), parsedHeader.Value.String()) } e.SetHeader("From", s.SMTP.FromAddress) - e.SetHeader("To", s.Email) + e.SetHeader("To", s.FormatAddress()) // Parse the templates var subjBuff bytes.Buffer tmpl, err := template.New("text_template").Parse(s.Template.Subject)