From f3ddb7074a29583274d5b9cb2e5d53d777acf94a Mon Sep 17 00:00:00 2001 From: Jordan Date: Thu, 26 Jun 2014 19:55:56 -0500 Subject: [PATCH] api/campaigns now also gets the Events and Template for the campaign(s) Added templating functionality (TODO: Put the logic in a function to reduce boilerplate) --- models/campaign.go | 18 +++++++++++++++++- worker/worker.go | 34 +++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/models/campaign.go b/models/campaign.go index 81ec8c21..9d687df3 100644 --- a/models/campaign.go +++ b/models/campaign.go @@ -76,7 +76,15 @@ func GetCampaigns(uid int64) ([]Campaign, error) { for i, _ := range cs { err := db.Model(&cs[i]).Related(&cs[i].Results).Error if err != nil { - fmt.Println(err) + Logger.Println(err) + } + err = db.Model(&cs[i]).Related(&cs[i].Events).Error + if err != nil { + Logger.Println(err) + } + err = db.Table("templates").Where("id=?", cs[i].TemplateId).Find(&cs[i].Template).Error + if err != nil { + Logger.Println(err) } } return cs, err @@ -90,6 +98,14 @@ func GetCampaign(id int64, uid int64) (Campaign, error) { return c, err } err = db.Model(&c).Related(&c.Results).Error + if err != nil { + return c, err + } + err = db.Model(&c).Related(&c.Events).Error + if err != nil { + return c, err + } + err = db.Table("templates").Where("id=?", c.TemplateId).Find(&c.Template).Error return c, err } diff --git a/worker/worker.go b/worker/worker.go index 5fed33a0..eb570938 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -1,10 +1,12 @@ package worker import ( + "bytes" "log" "net/smtp" "os" "strings" + "text/template" "github.com/jordan-wright/email" "github.com/jordan-wright/gophish/models" @@ -40,22 +42,37 @@ func processCampaign(c *models.Campaign) { e := email.Email{ Subject: c.Template.Subject, From: c.SMTP.FromAddress, - Text: []byte(c.Template.Text), - HTML: []byte(c.Template.HTML), } - Logger.Println(c.SMTP.Username) - Logger.Println(c.SMTP.Password) - Logger.Println(c.SMTP.FromAddress) var auth smtp.Auth if c.SMTP.Username != "" && c.SMTP.Password != "" { auth = smtp.PlainAuth("", c.SMTP.Username, c.SMTP.Password, strings.Split(c.SMTP.Host, ":")[0]) } for _, t := range c.Results { + // Parse the templates + var buff bytes.Buffer + tmpl, err := template.New("html_template").Parse(c.Template.HTML) + if err != nil { + Logger.Println(err) + } + err = tmpl.Execute(&buff, t) + if err != nil { + Logger.Println(err) + } + e.HTML = buff.Bytes() + buff.Reset() + tmpl, err = template.New("text_template").Parse(c.Template.Text) + if err != nil { + Logger.Println(err) + } + err = tmpl.Execute(&buff, t) + if err != nil { + Logger.Println(err) + } + e.Text = buff.Bytes() + buff.Reset() Logger.Println("Creating email using template") e.To = []string{t.Email} - Logger.Println(e.To) - Logger.Println(e.From) - err := e.Send(c.SMTP.Host, auth) + err = e.Send(c.SMTP.Host, auth) if err != nil { Logger.Println(err) err = t.UpdateStatus("Error") @@ -68,7 +85,6 @@ func processCampaign(c *models.Campaign) { Logger.Println(err) } } - Logger.Printf("Sending Email to %s\n", t.Email) } }