Adding ability to use template variables in the campaign URL. Fixes #844

pull/919/merge
Jordan Wright 2018-01-13 17:49:42 -06:00
parent d7810ddd2b
commit e995b0fcb7
4 changed files with 90 additions and 4 deletions

View File

@ -69,6 +69,12 @@ func (s *SendTestEmailRequest) Generate(msg *gomail.Message) error {
} }
msg.SetAddressHeader("From", f.Address, f.Name) 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 // Parse the customHeader templates
for _, header := range s.SMTP.Headers { for _, header := range s.SMTP.Headers {
key, err := buildTemplate(header.Key, s) key, err := buildTemplate(header.Key, s)

View File

@ -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.Text), check.Equals, string(expected.Text))
ch.Assert(string(got.HTML), check.Equals, string(expected.HTML)) 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)
}

View File

@ -213,6 +213,10 @@ func (m *MailLog) Generate(msg *gomail.Message) error {
fn = f.Address fn = f.Address
} }
msg.SetAddressHeader("From", f.Address, f.Name) msg.SetAddressHeader("From", f.Address, f.Name)
url, err := buildTemplate(c.URL, r)
if err != nil {
return err
}
td := struct { td := struct {
Result Result
URL string URL string
@ -221,9 +225,9 @@ func (m *MailLog) Generate(msg *gomail.Message) error {
From string From string
}{ }{
r, r,
c.URL + "?rid=" + r.RId, url + "?rid=" + r.RId,
c.URL + "/track?rid=" + r.RId, url + "/track?rid=" + r.RId,
"<img alt='' style='display: none' src='" + c.URL + "/track?rid=" + r.RId + "'/>", "<img alt='' style='display: none' src='" + url + "/track?rid=" + r.RId + "'/>",
fn, fn,
} }
@ -307,7 +311,7 @@ func LockMailLogs(ms []*MailLog, lock bool) error {
tx := db.Begin() tx := db.Begin()
for i := range ms { for i := range ms {
ms[i].Processing = lock ms[i].Processing = lock
err := tx.Debug().Save(ms[i]).Error err := tx.Save(ms[i]).Error
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err

View File

@ -238,3 +238,40 @@ func (s *ModelsSuite) TestUnlockAllMailLogs(ch *check.C) {
ch.Assert(m.Processing, check.Equals, false) 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)
}