diff --git a/models/campaign.go b/models/campaign.go index 460fd37d..8f16a46e 100644 --- a/models/campaign.go +++ b/models/campaign.go @@ -140,6 +140,11 @@ func (c *Campaign) getDetails() error { c.Template = Template{Name: "[Deleted]"} Logger.Printf("%s: template not found for campaign\n", err) } + err = db.Where("template_id=?", c.Template.Id).Find(&c.Template.Attachments).Error + if err != nil && err != gorm.ErrRecordNotFound { + Logger.Println(err) + return err + } err = db.Table("pages").Where("id=?", c.PageId).Find(&c.Page).Error if err != nil { if err != gorm.ErrRecordNotFound { diff --git a/worker/worker.go b/worker/worker.go index b7e61f2d..c9a7c1b6 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -129,8 +129,6 @@ func processCampaign(c *models.Campaign) { } // Parse the templates var subjBuff bytes.Buffer - var htmlBuff bytes.Buffer - var textBuff bytes.Buffer tmpl, err := template.New("text_template").Parse(c.Template.Subject) if err != nil { Logger.Println(err) @@ -142,24 +140,34 @@ func processCampaign(c *models.Campaign) { e.SetHeader("Subject", subjBuff.String()) Logger.Println("Creating email using template") e.SetHeader("To", t.Email) - tmpl, err = template.New("text_template").Parse(c.Template.Text) - if err != nil { - Logger.Println(err) + if c.Template.Text != "" { + var textBuff bytes.Buffer + tmpl, err = template.New("text_template").Parse(c.Template.Text) + if err != nil { + Logger.Println(err) + } + err = tmpl.Execute(&textBuff, td) + if err != nil { + Logger.Println(err) + } + e.SetBody("text/plain", textBuff.String()) } - err = tmpl.Execute(&textBuff, td) - if err != nil { - Logger.Println(err) + if c.Template.HTML != "" { + var htmlBuff bytes.Buffer + tmpl, err = template.New("html_template").Parse(c.Template.HTML) + if err != nil { + Logger.Println(err) + } + err = tmpl.Execute(&htmlBuff, td) + if err != nil { + Logger.Println(err) + } + if c.Template.Text == "" { + e.SetBody("text/html", htmlBuff.String()) + } else { + e.AddAlternative("text/html", htmlBuff.String()) + } } - e.SetBody("text/plain", textBuff.String()) - tmpl, err = template.New("html_template").Parse(c.Template.HTML) - if err != nil { - Logger.Println(err) - } - err = tmpl.Execute(&htmlBuff, td) - if err != nil { - Logger.Println(err) - } - e.AddAlternative("text/html", htmlBuff.String()) // Attach the files for _, a := range c.Template.Attachments { e.Attach(func(a models.Attachment) (string, gomail.FileSetting) {