mirror of https://github.com/gophish/gophish
Adding ability to use template variables in the campaign URL. Fixes #844
parent
d7810ddd2b
commit
e995b0fcb7
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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,
|
||||
"<img alt='' style='display: none' src='" + c.URL + "/track?rid=" + r.RId + "'/>",
|
||||
url + "?rid=" + r.RId,
|
||||
url + "/track?rid=" + r.RId,
|
||||
"<img alt='' style='display: none' src='" + 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
|
||||
|
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue