mirror of https://github.com/gophish/gophish
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)pull/24/head
parent
49da412538
commit
f3ddb7074a
|
@ -76,7 +76,15 @@ func GetCampaigns(uid int64) ([]Campaign, error) {
|
||||||
for i, _ := range cs {
|
for i, _ := range cs {
|
||||||
err := db.Model(&cs[i]).Related(&cs[i].Results).Error
|
err := db.Model(&cs[i]).Related(&cs[i].Results).Error
|
||||||
if err != nil {
|
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
|
return cs, err
|
||||||
|
@ -90,6 +98,14 @@ func GetCampaign(id int64, uid int64) (Campaign, error) {
|
||||||
return c, err
|
return c, err
|
||||||
}
|
}
|
||||||
err = db.Model(&c).Related(&c.Results).Error
|
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
|
return c, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package worker
|
package worker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"log"
|
"log"
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
"github.com/jordan-wright/email"
|
"github.com/jordan-wright/email"
|
||||||
"github.com/jordan-wright/gophish/models"
|
"github.com/jordan-wright/gophish/models"
|
||||||
|
@ -40,22 +42,37 @@ func processCampaign(c *models.Campaign) {
|
||||||
e := email.Email{
|
e := email.Email{
|
||||||
Subject: c.Template.Subject,
|
Subject: c.Template.Subject,
|
||||||
From: c.SMTP.FromAddress,
|
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
|
var auth smtp.Auth
|
||||||
if c.SMTP.Username != "" && c.SMTP.Password != "" {
|
if c.SMTP.Username != "" && c.SMTP.Password != "" {
|
||||||
auth = smtp.PlainAuth("", c.SMTP.Username, c.SMTP.Password, strings.Split(c.SMTP.Host, ":")[0])
|
auth = smtp.PlainAuth("", c.SMTP.Username, c.SMTP.Password, strings.Split(c.SMTP.Host, ":")[0])
|
||||||
}
|
}
|
||||||
for _, t := range c.Results {
|
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")
|
Logger.Println("Creating email using template")
|
||||||
e.To = []string{t.Email}
|
e.To = []string{t.Email}
|
||||||
Logger.Println(e.To)
|
err = e.Send(c.SMTP.Host, auth)
|
||||||
Logger.Println(e.From)
|
|
||||||
err := e.Send(c.SMTP.Host, auth)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Logger.Println(err)
|
Logger.Println(err)
|
||||||
err = t.UpdateStatus("Error")
|
err = t.UpdateStatus("Error")
|
||||||
|
@ -68,7 +85,6 @@ func processCampaign(c *models.Campaign) {
|
||||||
Logger.Println(err)
|
Logger.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.Printf("Sending Email to %s\n", t.Email)
|
Logger.Printf("Sending Email to %s\n", t.Email)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue