diff --git a/models/email_request.go b/models/email_request.go index 240101f9..242db417 100644 --- a/models/email_request.go +++ b/models/email_request.go @@ -69,6 +69,12 @@ func (s *SendTestEmailRequest) Generate(msg *gomail.Message) error { } msg.SetAddressHeader("From", f.Address, f.Name) + url, err := buildTemplate(s.URL, s) + if err != nil { + return err + } + s.URL = url + // Parse the customHeader templates for _, header := range s.SMTP.Headers { key, err := buildTemplate(header.Key, s) diff --git a/models/email_request_test.go b/models/email_request_test.go index 1317af90..9189a28a 100644 --- a/models/email_request_test.go +++ b/models/email_request_test.go @@ -93,3 +93,42 @@ func (s *ModelsSuite) TestEmailRequestGenerate(ch *check.C) { ch.Assert(string(got.Text), check.Equals, string(expected.Text)) ch.Assert(string(got.HTML), check.Equals, string(expected.HTML)) } + +func (s *ModelsSuite) TestEmailRequestURLTemplating(ch *check.C) { + smtp := SMTP{ + FromAddress: "from@example.com", + } + template := Template{ + Name: "Test Template", + Subject: "{{.URL}}", + Text: "{{.URL}}", + HTML: "{{.URL}}", + } + target := Target{ + FirstName: "First", + LastName: "Last", + Email: "firstlast@example.com", + } + req := &SendTestEmailRequest{ + SMTP: smtp, + Template: template, + Target: target, + URL: "http://127.0.0.1/{{.Email}}", + } + + msg := gomail.NewMessage() + err = req.Generate(msg) + ch.Assert(err, check.Equals, nil) + + expectedURL := fmt.Sprintf("http://127.0.0.1/%s", target.Email) + + msgBuff := &bytes.Buffer{} + _, err = msg.WriteTo(msgBuff) + ch.Assert(err, check.Equals, nil) + + got, err := email.NewEmailFromReader(msgBuff) + ch.Assert(err, check.Equals, nil) + ch.Assert(got.Subject, check.Equals, expectedURL) + ch.Assert(string(got.Text), check.Equals, expectedURL) + ch.Assert(string(got.HTML), check.Equals, expectedURL) +} \ No newline at end of file diff --git a/models/maillog.go b/models/maillog.go index 5eb1dffa..dce44ac4 100644 --- a/models/maillog.go +++ b/models/maillog.go @@ -213,6 +213,10 @@ func (m *MailLog) Generate(msg *gomail.Message) error { fn = f.Address } msg.SetAddressHeader("From", f.Address, f.Name) + url, err := buildTemplate(c.URL, r) + if err != nil { + return err + } td := struct { Result URL string @@ -221,9 +225,9 @@ func (m *MailLog) Generate(msg *gomail.Message) error { From string }{ r, - c.URL + "?rid=" + r.RId, - c.URL + "/track?rid=" + r.RId, - "", + url + "?rid=" + r.RId, + url + "/track?rid=" + r.RId, + "", fn, } @@ -307,7 +311,7 @@ func LockMailLogs(ms []*MailLog, lock bool) error { tx := db.Begin() for i := range ms { ms[i].Processing = lock - err := tx.Debug().Save(ms[i]).Error + err := tx.Save(ms[i]).Error if err != nil { tx.Rollback() return err diff --git a/models/maillog_test.go b/models/maillog_test.go index 835c42aa..b4f1787b 100644 --- a/models/maillog_test.go +++ b/models/maillog_test.go @@ -238,3 +238,40 @@ func (s *ModelsSuite) TestUnlockAllMailLogs(ch *check.C) { ch.Assert(m.Processing, check.Equals, false) } } + +func (s *ModelsSuite) TestURLTemplateRendering(ch *check.C) { + template := Template{ + Name: "URLTemplate", + UserId: 1, + Text: "{{.URL}}", + HTML: "{{.URL}}", + Subject: "{{.URL}}", + } + ch.Assert(PostTemplate(&template), check.Equals, nil) + campaign := s.createCampaignDependencies(ch) + campaign.URL = "http://127.0.0.1/{{.Email}}/" + campaign.Template = template + + ch.Assert(PostCampaign(&campaign, campaign.UserId), check.Equals, nil) + result := campaign.Results[0] + expectedURL := fmt.Sprintf("http://127.0.0.1/%s/?rid=%s", result.Email, result.RId) + + m := &MailLog{} + err := db.Where("r_id=? AND campaign_id=?", result.RId, campaign.Id). + Find(m).Error + ch.Assert(err, check.Equals, nil) + + msg := gomail.NewMessage() + err = m.Generate(msg) + ch.Assert(err, check.Equals, nil) + + msgBuff := &bytes.Buffer{} + _, err = msg.WriteTo(msgBuff) + ch.Assert(err, check.Equals, nil) + + got, err := email.NewEmailFromReader(msgBuff) + ch.Assert(err, check.Equals, nil) + ch.Assert(got.Subject, check.Equals, expectedURL) + ch.Assert(string(got.Text), check.Equals, expectedURL) + ch.Assert(string(got.HTML), check.Equals, expectedURL) +} \ No newline at end of file