Cleaning up some email sending bugs. Fixes #329

pull/336/head
Jordan Wright 2016-08-02 23:18:53 -05:00
parent 0d8a793550
commit b403e97eee
2 changed files with 31 additions and 18 deletions

View File

@ -140,6 +140,11 @@ func (c *Campaign) getDetails() error {
c.Template = Template{Name: "[Deleted]"} c.Template = Template{Name: "[Deleted]"}
Logger.Printf("%s: template not found for campaign\n", err) 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 err = db.Table("pages").Where("id=?", c.PageId).Find(&c.Page).Error
if err != nil { if err != nil {
if err != gorm.ErrRecordNotFound { if err != gorm.ErrRecordNotFound {

View File

@ -129,8 +129,6 @@ func processCampaign(c *models.Campaign) {
} }
// Parse the templates // Parse the templates
var subjBuff bytes.Buffer var subjBuff bytes.Buffer
var htmlBuff bytes.Buffer
var textBuff bytes.Buffer
tmpl, err := template.New("text_template").Parse(c.Template.Subject) tmpl, err := template.New("text_template").Parse(c.Template.Subject)
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
@ -142,6 +140,8 @@ func processCampaign(c *models.Campaign) {
e.SetHeader("Subject", subjBuff.String()) e.SetHeader("Subject", subjBuff.String())
Logger.Println("Creating email using template") Logger.Println("Creating email using template")
e.SetHeader("To", t.Email) e.SetHeader("To", t.Email)
if c.Template.Text != "" {
var textBuff bytes.Buffer
tmpl, err = template.New("text_template").Parse(c.Template.Text) tmpl, err = template.New("text_template").Parse(c.Template.Text)
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
@ -151,6 +151,9 @@ func processCampaign(c *models.Campaign) {
Logger.Println(err) Logger.Println(err)
} }
e.SetBody("text/plain", textBuff.String()) e.SetBody("text/plain", textBuff.String())
}
if c.Template.HTML != "" {
var htmlBuff bytes.Buffer
tmpl, err = template.New("html_template").Parse(c.Template.HTML) tmpl, err = template.New("html_template").Parse(c.Template.HTML)
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
@ -159,7 +162,12 @@ func processCampaign(c *models.Campaign) {
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
} }
if c.Template.Text == "" {
e.SetBody("text/html", htmlBuff.String())
} else {
e.AddAlternative("text/html", htmlBuff.String()) e.AddAlternative("text/html", htmlBuff.String())
}
}
// Attach the files // Attach the files
for _, a := range c.Template.Attachments { for _, a := range c.Template.Attachments {
e.Attach(func(a models.Attachment) (string, gomail.FileSetting) { e.Attach(func(a models.Attachment) (string, gomail.FileSetting) {