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
Jordan 2014-06-26 19:55:56 -05:00
parent 49da412538
commit f3ddb7074a
2 changed files with 42 additions and 10 deletions

View File

@ -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
}

View File

@ -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)
}
}